[英]NSCoding encode Array of Objects
I need to encode an array containing multiple instances of a custom NSObject
class. 我需要编码一个包含自定义
NSObject
类的多个实例的数组。 However, upon doing so, it returns a crash with message : 但是,在执行此操作时,它会返回包含以下消息的崩溃:
-[Person encodeWithCoder:]: unrecognized selector sent to instance 0x8ff2c50
- [Person encodeWithCoder:]:无法识别的选择器发送到实例0x8ff2c50
the class contains multiple properties and to store them as a collection is the purpose of the class. 该类包含多个属性,并将它们存储为集合是该类的目的。
the encoder method upon which it crashes is such: 崩溃的编码器方法是这样的:
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:_arrayOfPeople forKey:@"DataStoragePeopleArray"];
}
You need to implement the NSCoding
protocol in your Person
class. 您需要在
Person
类中实现NSCoding
协议。 Any custom class you wish to encode, including when it is contained in a collection you are encoding, needs to implement NSCoding
. 您希望编码的任何自定义类(包括何时包含在您正在编码的集合中)都需要实现
NSCoding
。
You'll need to implement encodeWithCoder:
and initWithCoder:
. 你需要实现
encodeWithCoder:
和initWithCoder:
.
Here is the documentation for the NSCoding
protocol: 以下是
NSCoding
协议的文档:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Protocols/NSCoding_Protocol/Reference/Reference.html https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Protocols/NSCoding_Protocol/Reference/Reference.html
I hope it will work..... 我希望它能奏效.....
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_arrayOfPeople forKey:@"DataStoragePeopleArray"];
}
-(id)initWithCoder:(NSCoder *)aCoder
{
if(self = [super init]){
self.yourpoperty = [aCoder decodeObjectforKey:@"DataStoragePeopleArray"];
}
return self;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.