如何以特定格式将数据从解析保存到NSArray

时间:2014-12-05 04:01:44

标签: objective-c parse-platform nsarray pfquery

我设置了sampleData数组的属性,如下所示:

@property (strong, nonatomic) NSArray *sampleData;

我希望这个数组的格式如下:

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

我的问题是实际数据来自 parse.com 数据库,我不知道如何以该格式将其保存到我的sampleData中。

以下是我到目前为止所做的事情,但仍缺少将数据从parse保存到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}"
 )

1 个答案:

答案 0 :(得分:0)

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;