-(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协议(protocol):

- (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;
}

基本上,您指定应该写入(编码)和从文件读取(解码)的内容。通常,对于要存储在文件中的每个属性,您所做的与我在示例中所做的相同。

关于ios - 存档数据时将无法识别的选择器发送到实例(NSCoding),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22168753/

10-10 20:46