简体   繁体   中英

Getting Last Object From Array in PFQuery (Parse)

I'm querying a class and key from parse. The end result should get the last object in the key text from the array userPostsObjects

Problem is NSLog(@"FINAL OUTPUT %@", msgObject[@"text"]); returns the whole array in the key text (hundreds of objects) not the last object

PFQuery *queryChatClass = [PFQuery queryWithClassName:@"Chat"];

[queryChatClass selectKeys:@[@"text",
                             @"user"]];

[queryChatClass findObjectsInBackgroundWithBlock:^(NSArray *userPostsObjects, NSError *error) {
    if (!error) {

        // Do something with the data
        for (PFObject *msgObject in userPostsObjects) {

            NSLog(@"FINAL OUTPUT %@", msgObject[@"text"]);

        }
    }
    else {

        // Log details of the failure
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];

My best guess was to use for (PFObject *msgObject in [userPostsObjects lastObject])

However this results in: 'NSInvalidArgumentException', reason: '-[PFObject countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x14567300'

If you use lastObject , you can just "do something with the data" without iterating (you have just one object, not an array); ie:

NSLog(@"FINAL OUTPUT %@", [userPostsObjects lastObject][@"text"]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM