0

我创建了一个名为 UserInfo 的 PFObject。一切都正确保存到 Parse,但是当我去检索它时,我不断收到错误。下面是我的代码。

PFQuery *query = [PFQuery queryWithClassName:@"UserInfo"];
[query whereKey:@"user" equalTo:currentUser];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (error) {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    } else {
        self.userInfo = objects;
        NSLog(@"%@", self.userInfo);

        self.locationDisplay.text = [self.userInfo valueForKey:@"location"];

    }
}];

错误的 NSLog 输出如下:

-[__NSArrayI length]: unrecognized selector sent to instance 0x9aa2be0 

提前感谢您的帮助!

4

1 回答 1

1

该查询findObjectsInBackgroundWithBLock:将数组存储在objects.

完成此操作后,您将属性设置userInfo为使用以下行指向该数组

self.userInfo = objects;

所以基本上在这里,self.userInfo持有对数组的引用。

当您尝试设置标签时,您valueForKey:直接在数组上调用该方法。我相信你想在这个数组中的一个对象上调用这个方法。

您可以尝试将行更改为:

self.locationDisplay.text = [[self.userInfo firstObject] valueForKey:@"location"];

它将@"location"在数组的第一个对象上查找键的值,该对象应该是PFObject.

注意:在调用此行之前,您应该首先测试它objects不是一个空数组,否则如果数组为空,您可能会尝试调用valueForKey:一个nil对象。

于 2014-08-05T15:25:25.440 回答