【问题标题】:Adding Data to NSArray from Parse query从 Parse 查询向 NSArray 添加数据
【发布时间】:2015-03-03 05:44:29
【问题描述】:

所以我的应用程序在键 firstName 中存储了朋友的名字,在键 lastName 中存储了姓氏,在名为 friendsAssociation 的解析类中存储了用户名,此外它还将登录用户的用户名存储在名为 user 的对象中.我想查询数据以查找用户对象等于用户用户名的所有实例。然后我想将朋友的名字存储在一个数组中。

这是我尝试使用的代码的副本

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:_user];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        // The find succeeded.
        NSLog(@"Successfully retrieved %d users.", objects.count);
        // Do something with the found objects
        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 an object matching the query
        if (objects.count >=1) {
            for (PFObject *object in objects) {
                NSLog(@"%@", objects);}
            firstname[objects.count]= [object objectforkey:@"firstName"];
       }
        //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]);
    }
 }];

具体来说,我想使用这个 for 循环将数据存储在名为 firstname 的 NSArray

   for (PFObject *object in objects) {
      NSLog(@"%@", objects);}
      firstname[objects.count]= [object objectforkey:@"firstName"];
   }

但是,我无法拥有[object objectforkey:@"firstName"] 行,因为未声明对象。我该如何解决这个问题?

【问题讨论】:

    标签: ios objective-c arrays parse-platform nsstring


    【解决方案1】:

    您的for 循环中有语法问题(甚至在使用object 之前用} 关闭它)。

    试试这个:

    for (PFObject *object in objects) {
        NSLog(@"%@", object);
        [firstname addObject:[object objectforkey:@"firstName"]];
    }
    

    编辑:通过这样做,您可能需要平衡关闭花括号的删除与代码中较早的另一个打开的花括号...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-19
      • 2010-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多