hanumanDev
hanumanDev

Reputation: 6614

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

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}"
)

Upvotes: 1

Views: 1903

Answers (2)

Ponting
Ponting

Reputation: 2246

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.

Upvotes: 4

lxt
lxt

Reputation: 31304

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.

Upvotes: 4

Related Questions