Parse IOS stringWithFormat only adding one set of data to NSMutableArray instead of two sets

106 Views Asked by At

For my code I query parse for a firstName and lastName, I then place both of these values in strings and then i combine the strings and add a space in the middle. I then add this new string to an NSMutableArray. However, only the first instances is being added to the array. All other arrays as shown in the code get the correct number of data values. Here is my code.

   for (PFObject *object in objects) {
                NSLog(@"%@", objects[i]);
                NSString *phonenumber = object[@"phoneNumber"];
                NSString *firstname = object[@"firstName"];
                NSString *lastname = object [@"lastName"];
                NSString *email = object [@"email"];
                NSString *combinedname = [NSString stringWithFormat:@"%@ %@", firstname, lastname];
                NSLog(@"%@",combinedname);
                NSLog(@"%@", phonenumber);
                [phoneNUMBERS addObject:phonenumber];
                [firstNAMES addObject:firstname];
                [lastNAMES addObject:lastname];
                [EMAILS addObject:email];
                [combinedNAMES addObject:combinedname];
                  NSLog(@"y0");
                NSLog(@"%@",combinedNAMES);
            }

Is there any reason why combinedNAMES only gets the first set of first and last names and not the second?

So for example the two firstNames stored in parse are Jack and John, and their last names are Apple and Gardner. The array only stores the name Jack Apple and does not store John Gardner. However, the array containing the first names contains both Jack and John

-(IBAction)displayfriendinfo:(id)sender{
    phoneNUMBERS = [NSMutableArray new];
    firstNAMES =[NSMutableArray new];
    lastNAMES = [NSMutableArray new];
    EMAILS = [NSMutableArray new];
    combinedNAMES = [[NSMutableArray alloc] init];
// Initialize table data
    PFQuery *query = [PFQuery queryWithClassName:@"friendsAssociation"];
//quesrys the class Friend asssociation to find when instances of "user" equal the
//logged in user's username
     [query whereKey:@"user" equalTo:usernamecontrol];
     [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            // The find succeeded.
            NSLog(@"Successfully retrieved %d users.", objects.count);
           // Do something with the found objects
            ContactTableViewController *contacts =   [[ContactTableViewController alloc]initWithNibName:Nil bundle:Nil];
            contacts.user = usernamecontrol;
            contacts.COMBINEDNAMES = [NSMutableArray new];
            contacts.FIRSTNAMES = [NSMutableArray new];
            contacts.PHONENUMBERS = [NSMutableArray new];
        contacts.COMBINEDNAMES = combinedNAMES;
        contacts.PHONENUMBERS = phoneNUMBERS;
        contacts.FIRSTNAMES = firstNAMES;

        [self presentViewController:contacts animated:YES completion:Nil];


        if (objects.count == 0) {
            //uialert letting the user know that no phone number matches the query
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No User"
                                                            message:@"No user matches this username"
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];








        }
        //if there is only one number matching the query

        if (objects.count >=1) {


            int i=0;

            for (PFObject *object in objects) {

                NSLog(@"%@", objects[i]);


                NSString *phonenumber = object[@"phoneNumber"];
                NSString *firstname = object[@"firstName"];
                NSString *lastname = object [@"lastName"];
                NSString *email = object [@"email"];

                NSString *combinedname = [NSString stringWithFormat:@"%@ %@", firstname, lastname];
                NSLog(@"%@",combinedname);




                NSLog(@"%@", phonenumber);


                [phoneNUMBERS addObject:phonenumber];
                [firstNAMES addObject:firstname];
                [lastNAMES addObject:lastname];
                [EMAILS addObject:email];
                [combinedNAMES addObject:combinedname];
                  NSLog(@"y0");
                NSLog(@"%@",combinedNAMES);
            }





            NSLog(@"y0");
            NSLog(@"%@", phoneNUMBERS);







            //if there is more than one phonenumber matching the query as
            //the user to input the friends username
            //instead


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

} Here is the whole method I am using if this helps spot the problem

1

There are 1 best solutions below

3
Saif On

Make sure that the combinedNAMES array is initialized outside the for loop. i.e.,

combinedNAMES = [[NSMutableArray alloc] init];
for (PFObject *object in objects) {
            NSLog(@"%@", objects[i]);
            NSString *phonenumber = object[@"phoneNumber"];
            NSString *firstname = object[@"firstName"];
            NSString *lastname = object [@"lastName"];
            NSString *email = object [@"email"];
            NSString *combinedname = [NSString stringWithFormat:@"%@ %@", firstname, lastname];
            NSLog(@"%@",combinedname);
            NSLog(@"%@", phonenumber);
            [phoneNUMBERS addObject:phonenumber];
            [firstNAMES addObject:firstname];
            [lastNAMES addObject:lastname];
            [EMAILS addObject:email];
            [combinedNAMES addObject:combinedname];
              NSLog(@"y0");
            NSLog(@"%@",combinedNAMES);
 }