2014-12-05 63 views
-1

我設置的sampleData的陣列的特性如下:如何從解析數據保存到一個NSArray的特定格式

@property (strong, nonatomic) NSArray *sampleData; 

而且我想這個數組有一個格式是這樣的:

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

我的問題是,實際的數據將來自parse.com數據庫,我不知道如何將這個格式保存到我的sampleData中。

這裏下面是我已經走了一下,到目前爲止這仍然是缺少的一部分,從解析數據保存到的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}" 
) 

回答

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; 
+0

嗨@rickerbh即使我接受了你對這個問題的回答,我意識到我的sampleData現在需要一些分組來對齊我的編碼,以便將這些數據顯示到視圖中。這裏是我的進一步問題鏈接[鏈接](http://stackoverflow.com/questions/27317469/add-objects-to-nsmutable-array-with-grouping)。希望你有興趣幫助我。 – SanitLee 2014-12-05 17:25:10