Objective C: How to resolve 'unrecognized selector sent to instance' error
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
That error means that not everything in your likersArray
is a User
object. At least one thing in there is an NSString
.
It could also mean that one of the User
objects in likersArray
is being over-released and you are hitting garbage.
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!
精彩评论