从Parse查询向NSArray添加数据

时间:2015-03-03 05:44:29

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

所以我的应用会在密钥firstName中存储朋友的名字,密钥lastName中的姓氏,以及名为friendsAssociation的解析类中的用户名,此外它还会存储已登录的用户& #39;名为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循环将数据存储在NSArray名为firstname

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

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

1 个答案:

答案 0 :(得分:0)

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

试试这个:

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

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