Extracting data from a Parse.com class and into an NSArray

余生长醉 提交于 2019-12-23 04:24:59

问题


I'm using the following query to get some data from a Parse.com class.

What I would like to do is extract the Rating object from the NSArray rateObjects. How would I go about doing this?

thanks for any help

 PFQuery *rateQuery = [PFQuery queryWithClassName:@"Rating"];
        [rateQuery whereKey:@"photo" equalTo:self.photo];
        [rateQuery includeKey:@"photo"];

    rateQuery.cachePolicy = kPFCachePolicyNetworkElseCache;
    rateQuery.limit = 20;

    [rateQuery findObjectsInBackgroundWithBlock:^(NSArray *rateObjects, NSError *error)
     {
         if( !error )
         {
             NSLog(@"rateObject %@", rateObjects);

         }
     }
     ];

Here's the NSLog output:

rateObject (
    "<Rating:w9ENTO29mA:(null)> {\n    ACL = \"<PFACL: 0x1e0a5380>\";\n    Rating = 4;\n    fromUser = \"<PFUser:uV2xu0c3ec>\";\n    photo = \"<Photo:Rv4qqrHUPr>\";\n    toUser = \"<PFUser:uV2xu0c3ec>\";\n    user = \"<PFUser:uV2xu0c3ec>\";\n}",
    "<Rating:t3pjtehYR0:(null)> {\n    ACL = \"<PFACL: 0x1e0f9f90>\";\n    Rating = 5;\n    fromUser = \"<PFUser:uV2xu0c3ec>\";\n    photo = \"<Photo:Rv4qqrHUPr>\";\n    toUser = \"<PFUser:uV2xu0c3ec>\";\n    user = \"<PFUser:uV2xu0c3ec>\";\n}"
)

回答1:


Your NSArray will contain PFObjects, which you can treat in a similar way to a dictionary. In the query you ran above you received two rating objects back. If that's not what you wanted (you only wanted a single object) you may want to revisit how you're querying your data.

Assuming your Rating class in Parse contains a key called Rating you would access it like this:

[rateObject objectForKey:@"Rating"]

You can also use the new modern literal syntax if you like - rateObject[@"rating"]

You'll need to iterate through your array to view all the rating objects that have been returned, so you'll probably end up with something like this:

for (id item in rateObjects) {
    int ratingVal = [[item objectForKey:@"Rating"] intValue];
    NSLog(@"Rating: %d", ratingVal);
}

You may find Parse's iOS documentation helpful - and if you're not sure what the code above actually does, you may want to review arrays and how they work in Objective-C.




回答2:


Try to use this:

[rateQuery findObjectsInBackgroundWithBlock:^(NSArray *rateObjects, NSError *error)
 {
     if( !error )
     {
       NSMutableArray *data = [[NSMutableArray alloc]init];
      [data addObjectsFromArray:rateObjects];
       NSArray *rating_data = [data valueForKey:@"Rating"];
       NSLog(@"%@",[rating_data objectAtIndex:0]);

     }
 }
];

I hope this will help you.



来源:https://stackoverflow.com/questions/16958939/extracting-data-from-a-parse-com-class-and-into-an-nsarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!