Objective C: How to resolve 'unrecognized selector sent to instance' error

╄→尐↘猪︶ㄣ 提交于 2019-12-12 17:07:42

问题


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

回答1:


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




回答2:


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




回答3:


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!



来源:https://stackoverflow.com/questions/6722149/objective-c-how-to-resolve-unrecognized-selector-sent-to-instance-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!