1

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

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the post. Using the following I get the output "Rating:0". However in the NSArray output I get Rating = 4. NSDictionary *rateObject; int ratingVal = [[rateObject objectForKey:@"Rating"] intValue]; NSLog(@"Rating: %d", ratingVal);
There was a typo in the code - sorry. I accidentally had you called objectForKey on rateObjects, it should be item. Try it again with the modified version (I've corrected my answer)
that worked perfectly! thanks so much for your help. I was stuck on that for some time. :)
4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.