Search code examples
iosobjective-cparse-platform

Parse PFObject save results to NSMutableArray


I'm looking to save values within from the PFObjet and save them to multiple NSMutableArrays. Let me explain. I'm download information from the Partisipants class within Parse. The Partisipants class contains multiple NSString values, such as name, phoneNumber, etc. I'm looking to scrape that information from the return value of the query, and add each instance of a new objectID to the NSMutableArray. Assume there are multiple rows, or userIDs to scrape into the NSMutableArray.

PFQuery *query = [PFQuery queryWithClassName:@"Partisipants"];
    [query setLimit: 600];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            // The find succeeded. The first 600 objects are available in objects
            self.objectsFromParse = objects;
            NSLog(@"%@", self.objectsFromParse);
        } else {
            // Log details of the failure
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];

Edit

This is my return result for executing this query (personal information was modified):

(
    "<Partisipants:S420zHiNFT:(null)> {\n    additionalDonation = \"100.00\";\n    emailAddress = \"somewhere@email.com\";\n    emergencyPhone = 12345678901;\n    hasArrived = NO;\n    name = \"James\";\n    phoneNumber = 12345678901;\n}",
    "<Partisipants:9BpoFxO1zx:(null)> {\n    additionalDonation = \"500.00\";\n    emailAddress = \"someguy@email.com\";\n    emergencyPhone = 12345678901;\n    hasArrived = NO;\n    name = \"Michael\";\n    phoneNumber = 12345678901;\n}"
)

Solution

  • If you want to store emails, names, objectId's etc. each in new mutableArrays, you could do:

    NSMutableArray * usernames = [NSMutableArray array];
    NSMutableArray * emails = [NSMutableArray array];
    NSMutableArray * objectIds = [NSMutableArray array];
    for (PFObject * participant in objects) {
        if (participant[@"name"]) [usernames addObject:participant[@"name"]];
        if (participant[@"emailAddress"]) [emails addObject:participant[@"emailAddress"]];
        [objectIds addObject:participant.objectId];
    }