问题
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