【问题标题】:Parse IOS stringWithFormat only adding one set of data to NSMutableArray instead of two setsParse IOS stringWithFormat 只添加一组数据到 NSMutableArray 而不是两组
【发布时间】:2015-03-04 04:58:16
【问题描述】:

对于我的代码,我查询解析 firstNamelastName,然后将这两个值放在字符串中,然后合并字符串并在中间添加一个空格。然后我将这个新字符串添加到NSMutableArray。但是,只有第一个实例被添加到数组中。代码中显示的所有其他数组都获得正确数量的数据值。这是我的代码。

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

combinedNAMES 只得到第一组名字和姓氏而不是第二组有什么原因吗?

例如,存储在 parse 中的两个名字是 Jack 和 John,他们的姓氏是 Apple 和 Gardner。该数组仅存储名称 Jack Apple,不存储 John Gardner。但是,包含名字的数组同时包含 Jack 和 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]);
        }
    }}];

} 如果这有助于发现问题,这是我正在使用的整个方法

【问题讨论】:

  • 我无法理解“只得到第一组名字和姓氏而不是第二组”的意思
  • 例如,存储在 parse 中的两个名字是 Jack 和 John,他们的姓氏是 Apple 和 Gardner。该数组仅存储名称 Jack Apple,不存储 John Gardner。但是,包含名字的数组同时包含 Jack 和 John
  • NSLog 是否打印出所有组合名称?
  • 是的 NSLog 是 NSLog(@"%@",combinedname) 打印出另一个与 combineNAMES 没有

标签: ios objective-c parse-platform nsmutablearray stringwithformat


【解决方案1】:

确保combinedNAMES 数组在for 循环之外初始化。 即,

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

【讨论】:

  • 我用 combineNames = [NSMutableArray new];它存储一组数据但不存储另一组,所以我认为初始化不是问题
  • 你在哪里初始化的,我想你是在 for 循环中做的!
  • 我更新了问题以显示整个方法/代码
猜你喜欢
  • 1970-01-01
  • 2021-03-16
  • 2014-08-30
  • 1970-01-01
  • 2016-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多