-(void)transformObjects:(NSMutableArray*)array key:(NSString*)key { NSMutableArray* archiveArray = [[NSMutableArray alloc]initWithCapacity:array.count]; for (Furniture *furniture in array) { // The error occurs on the line below NSData *furnitureEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:furniture]; [archiveArray addObject:furnitureEncodedObject]; } NSUserDefaults *userData = [NSUserDefaults standardUserDefaults]; [userData setObject:archiveArray forKey:key]; }
错误日志:
2014-03-04 10:55:27.881 AppName[10641:60b] -[Furniture encodeWithCoder:]: unrecognized selector sent to instance 0x15d43350
我不知道为什么在尝试归档对象时会收到“无法识别的选择器发送到实例”.
解决方法
您需要在Furniture对象中实现NSCoding协议:
- (void)encodeWithCoder:(NSCoder *)aCoder{ [aCoder encodeObject:self.yourpoperty forKey:@"PROPERTY_KEY"]; } -(id)initWithCoder:(NSCoder *)aDecoder{ if(self = [super init]){ self.yourpoperty = [aDecoder decodeObjectForKey:@"PROPERTY_KEY"]; } return self; }