Objective C:如何解决“发送到实例的无法识别的选择器”问题错误

发布于 2024-11-24 21:39:41 字数 700 浏览 2 评论 0原文

我尝试使用以下代码访问实例对象的属性

for (User *user in likersArray) 
{
    //Set variables for dictionary
    NSString *nameLength = [NSString stringWithFormat:@"%i",[user.nickname length]];
}

但是,我不断收到以下错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString nickname]: unrecognized selector sent to instance 0x8c0f780'

我的用户类定义如下

@interface User : NSObject <NSCoding>
{
NSString *uid;
NSString *name;
NSString *nickname;
}

@property (nonatomic, copy) NSString *uid;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *nickname;

@end

I am trying to access a property of instance object using the following code

for (User *user in likersArray) 
{
    //Set variables for dictionary
    NSString *nameLength = [NSString stringWithFormat:@"%i",[user.nickname length]];
}

However, I keep getting the following error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString nickname]: unrecognized selector sent to instance 0x8c0f780'

My user class is defined as below

@interface User : NSObject <NSCoding>
{
NSString *uid;
NSString *name;
NSString *nickname;
}

@property (nonatomic, copy) NSString *uid;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *nickname;

@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空城仅有旧梦在 2024-12-01 21:39:42

这也可能意味着 likersArray 中的一个 User 对象被过度释放,并且您遇到了垃圾。

It could also mean that one of the User objects in likersArray is being over-released and you are hitting garbage.

这个俗人 2024-12-01 21:39:42

我遇到了一个非常类似的问题,这是由于只创建了一项并将其插入数组而引起的。如果您的 likersArray 仅包含 1 个项目,它也会导致此错误,并且这是一个很难找到的错误。希望这对某人有帮助!

I had a very similar issue which was being caused due to only one item being created and inserted into the array. If your likersArray contains only 1 item it causes this error as well and it's a nasty bug to find. Hopefully this helps someone!

眼泪都笑了 2024-12-01 21:39:41

该错误意味着并非 likersArray 中的所有内容都是 User 对象。其中至少有一个东西是一个NSString

That error means that not everything in your likersArray is a User object. At least one thing in there is an NSString.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文