将 PFObject 设置为 UILabel [英] Setting PFObject in to UILabel

查看:67
本文介绍了将 PFObject 设置为 UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a PFObject named UserInfo. Everything saves to Parse correctly, but when I go to retrieve it I keep getting errors. Here is my code below.

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 输出如下:

The NSLog out put for the error is as follows:

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

提前感谢您的帮助!

推荐答案

查询 findObjectsInBackgroundWithBLock:objects 中存储一个数组.

The query findObjectsInBackgroundWithBLock: stores an array in objects.

完成后,您将属性 userInfo 设置为指向该数组的行

Once this is done, you set your attribute userInfo to point to this array with the line

self.userInfo = objects;

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

So basically here, self.userInfo holds a reference to an array.

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

When you try to set your label, you call the method valueForKey: directly on the array. I believe you want to call this method on an object inside this array.

您可以尝试将行更改为:

What you could try is change the line to :

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

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

which will look for the value for the key @"location" on the first object of your array, which should be a PFObject.

注意:在调用此行之前,您应该首先测试 objects 不是空数组,否则您可能会尝试在 nil<上调用 valueForKey:/code> 对象,如果数组为空.

Note : you should first test that objects is not an empty array before calling this line, otherwise you may try to call valueForKey: on a nil object, if the array is empty.

这篇关于将 PFObject 设置为 UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆