NSCoding编码对象数组

时间:2014-09-05 11:09:34

标签: ios objective-c

我需要编码一个包含自定义NSObject类的多个实例的数组。但是,执行此操作时,它将返回崩溃消息:

  

- [Person encodeWithCoder:]:无法识别的选择器发送到实例0x8ff2c50

该类包含多个属性,并将它们存储为集合是该类的目的。

崩溃的编码器方法是这样的:

- (void)encodeWithCoder:(NSCoder *)encoder 
{
    [encoder encodeObject:_arrayOfPeople forKey:@"DataStoragePeopleArray"];
}

2 个答案:

答案 0 :(得分:3)

您需要在NSCoding课程中实施Person协议。您希望编码的任何自定义类,包括它在您编码的集合中包含的类,都需要实现NSCoding

您需要实施encodeWithCoder:initWithCoder:

以下是NSCoding协议的文档:

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Protocols/NSCoding_Protocol/Reference/Reference.html

答案 1 :(得分:0)

我希望它会起作用.....

-(void)encodeWithCoder:(NSCoder *)aCoder
  {
   [aCoder encodeObject:_arrayOfPeople forKey:@"DataStoragePeopleArray"];
  }

-(id)initWithCoder:(NSCoder *)aCoder
{
   if(self = [super init]){
   self.yourpoperty = [aCoder decodeObjectforKey:@"DataStoragePeopleArray"];
  }
   return self;
  }