簡體   English   中英

從Parse查詢向NSArray添加數據

[英]Adding Data to NSArray from Parse query

所以我的應用程序在密鑰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"]因為未聲明對象。 我怎樣才能解決這個問題?

你的for循環中有一個語法問題(在使用object之前用一個}關閉它)。

嘗試這個:

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

編輯 :通過這樣做,你可能必須平衡關閉花括號的刪除與你的代碼中早先打開的另一個花括號...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM