Как сохранить данные из разбора в NSArray в определенном формате

Я установил свойство массива sampleData следующим образом:

@property (strong, nonatomic) NSArray *sampleData;

И я хочу, чтобы этот массив имел примерно такой формат:

self.sampleData = @[ @{ @"createdAt": @"date A",
                        @"text":  @"text A"
                      },
                    @{ @"createdAt": @"date B",
                        @"text":  @"text B"
                      }
                ];

Моя проблема в том, что фактические данные будут поступать из базы данных parse.com, и я не знаю, как сохранить их в моем примере данных в указанном формате.

Ниже приведено то, что я сделал до сих пор, в котором все еще отсутствует часть для сохранения данных из синтаксического анализа в массив sampleData:

PFQuery *postQuery = [PFQuery queryWithClassName:kPAWParsePostsClassKey];
[postQuery whereKey:kPAWParseUserKey equalTo:[PFUser currentUser]];
[postQuery includeKey:kPAWParseUserKey];

postQuery.cachePolicy = kPFCachePolicyNetworkElseCache;
postQuery.limit = 20;

[postQuery findObjectsInBackgroundWithBlock:^(NSArray *myPosts, NSError *error)
 {
     if( !error )
     {
         NSLog(@"My Post --> %@", myPosts);

         //self.sampleData = mySpotPosts; //this is definitely not right so need help here

     }
 }
];

А вот вывод NSlog:

 My Post --> (
 "<Posts:QJzFvbkKPO:(null)> {\n    ACL = \"<PFACL: 0x195a0bd0>\";\n    location = \"<PFGeoPoint: 0x17af9da0>\";\n    text = \"Yuangi brunch\";\n    user = \"<PFUser:4689S87hx5>\";\n}",
 "<Posts:fhQAiGeVqI:(null)> {\n    ACL = \"<PFACL: 0x19368c90>\";\n    location = \"<PFGeoPoint: 0x1945ed00>\";\n    text = \"Nanliao harbor, hsinju\";\n    user = \"<PFUser:4689S87hx5>\";\n}",
 "<Posts:jT5x9ySzW6:(null)> {\n    ACL = \"<PFACL: 0x194b7510>\";\n    location = \"<PFGeoPoint: 0x19565630>\";\n    text = Nanjuang;\n    user = \"<PFUser:4689S87hx5>\";\n}",
 "<Posts:5iMO83El7z:(null)> {\n    ACL = \"<PFACL: 0x19542fc0>\";\n    location = \"<PFGeoPoint: 0x194bc620>\";\n    text = \"Carrefour, Neihu\";\n    user = \"<PFUser:4689S87hx5>\";\n}",
 "<Posts:51HRgSopVN:(null)> {\n    ACL = \"<PFACL: 0x1940d3b0>\";\n    location = \"<PFGeoPoint: 0x19467510>\";\n    text = \"Www.thecubeinn.com.tw\";\n    user = \"<PFUser:4689S87hx5>\";\n    whoLiked =     (\n    );\n}",
 "<Posts:DyRBc0iIyW:(null)> {\n    ACL = \"<PFACL: 0x19493360>\";\n    location = \"<PFGeoPoint: 0x194d65a0>\";\n    text = \"Www.thecubeinn.com.tw\";\n    user = \"<PFUser:4689S87hx5>\";\n    whoLiked =     (\n    );\n}",
 "<Posts:PgmZdTLIVt:(null)> {\n    ACL = \"<PFACL: 0x193c81b0>\";\n    location = \"<PFGeoPoint: 0x19350b20>\";\n    text = \"Mei Hua Hu Lake\";\n    user = \"<PFUser:4689S87hx5>\";\n}",
 "<Posts:LUBu0s5Bkt:(null)> {\n    ACL = \"<PFACL: 0x1949b120>\";\n    location = \"<PFGeoPoint: 0x195e35a0>\";\n    text = \"Surprisingly very good food here\";\n    user = \"<PFUser:4689S87hx5>\";\n}",
 "<Posts:6fHRjfO36A:(null)> {\n    ACL = \"<PFACL: 0x194dca40>\";\n    location = \"<PFGeoPoint: 0x195ca270>\";\n    text = \"Village Churasco\";\n    user = \"<PFUser:4689S87hx5>\";\n}",
 "<Posts:4PeJdGSpOw:(null)> {\n    ACL = \"<PFACL: 0x165a21f0>\";\n    location = \"<PFGeoPoint: 0x195be6a0>\";\n    text = \"Wang long pei \";\n    user = \"<PFUser:4689S87hx5>\";\n}",
 "<Posts:7cYuel47k8:(null)> {\n    ACL = \"<PFACL: 0x193f7760>\";\n    location = \"<PFGeoPoint: 0x195d7660>\";\n    text = \"R den dessert factory\";\n    user = \"<PFUser:4689S87hx5>\";\n}",
 "<Posts:vOGFIotiOc:(null)> {\n    ACL = \"<PFACL: 0x193ce0b0>\";\n    imageFile = \"<PFFile: 0x195a21a0>\";\n    imageFileTH = \"<PFFile: 0x16560c10>\";\n    location = \"<PFGeoPoint: 0x19532620>\";\n    text = \"Home pic2\";\n    user = \"<PFUser:4689S87hx5>\";\n}",
 "<Posts:SDdAJxgIvF:(null)> {\n    ACL = \"<PFACL: 0x195e5a70>\";\n    imageFile = \"<PFFile: 0x17ae3800>\";\n    imageFileTH = \"<PFFile: 0x1651c4f0>\";\n    location = \"<PFGeoPoint: 0x19350320>\";\n    text = \"Home pic3\";\n    user = \"<PFUser:4689S87hx5>\";\n}",
 "<Posts:t6VBfj0Dv1:(null)> {\n    ACL = \"<PFACL: 0x195c7860>\";\n    imageFile = \"<PFFile: 0x1655c5b0>\";\n    imageFileTH = \"<PFFile: 0x195a6460>\";\n    location = \"<PFGeoPoint: 0x17a25ba0>\";\n    text = \"Home pic4\";\n    user = \"<PFUser:4689S87hx5>\";\n}",
 "<Posts:nUtog76u7q:(null)> {\n    ACL = \"<PFACL: 0x195d9050>\";\n    imageFile = \"<PFFile: 0x17a507f0>\";\n    imageFileTH = \"<PFFile: 0x19542e30>\";\n    location = \"<PFGeoPoint: 0x195e0850>\";\n    text = \"Home pic5\";\n    user = \"<PFUser:4689S87hx5>\";\n}",
 "<Posts:XxOLjsdO5u:(null)> {\n    ACL = \"<PFACL: 0x195d7450>\";\n    imageFile = \"<PFFile: 0x17a6a2f0>\";\n    imageFileTH = \"<PFFile: 0x195dcb20>\";\n    location = \"<PFGeoPoint: 0x195d9950>\";\n    text = \"Home pic6\";\n    user = \"<PFUser:4689S87hx5>\";\n}",
 "<Posts:UauisYVrQX:(null)> {\n    ACL = \"<PFACL: 0x19582e20>\";\n    imageFile = \"<PFFile: 0x194b7b00>\";\n    imageFileTH = \"<PFFile: 0x19314340>\";\n    location = \"<PFGeoPoint: 0x19327550>\";\n    text = \"Home pic7\";\n    user = \"<PFUser:4689S87hx5>\";\n}",
 "<Posts:TvUu5q4ijc:(null)> {\n    ACL = \"<PFACL: 0x195504b0>\";\n    location = \"<PFGeoPoint: 0x194c8de0>\";\n    text = \"Yana's home\";\n    user = \"<PFUser:4689S87hx5>\";\n    whoLiked =     (\n        4689S87hx5\n    );\n}",
 "<Posts:u2DoXMia6G:(null)> {\n    ACL = \"<PFACL: 0x1934b6d0>\";\n    location = \"<PFGeoPoint: 0x1934f730>\";\n    text = \"The Cube inn\";\n    user = \"<PFUser:4689S87hx5>\";\n}",
 "<Posts:rijY1Y4jQI:(null)> {\n    ACL = \"<PFACL: 0x16573090>\";\n    location = \"<PFGeoPoint: 0x1941ae20>\";\n    text = \"The Cube inn1\";\n    user = \"<PFUser:4689S87hx5>\";\n}"
 )

person SanitLee    schedule 05.12.2014    source источник


Ответы (1)


Тело вашего if (!error) {... должно быть примерно таким:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy hh:mm"]; // Set to whatever format you want your dates to be
NSMutableArray *objectArray = [NSMutableArray new];
for (PFObject *object in myPosts) {
  [objectArray addObject:@{@"createdAt": [formatter stringFromDate:object.createdAt], @"text": [object objectForKey:@"text"]}];
}
self.sampleData = objectArray;
person rickerbh    schedule 05.12.2014
comment
Привет, @rickerbh, несмотря на то, что я принял ваш ответ на этот вопрос, я понял, что мой образец данных теперь требует некоторой группировки, чтобы соответствовать моему коду для отображения этих данных в представлении. Вот ссылка ссылка на мой дополнительный вопрос. Надеюсь, вам будет интересно помочь мне. - person SanitLee; 05.12.2014