简体   繁体   English

NSCoding与嵌套自定义对象?

[英]NSCoding with Nested Custom Objects?

I have a series of nested objects that I am needing to put through the NSCoding protocol so that I can save the top level object into NSUserDefaults. 我需要通过NSCoding协议放置一系列嵌套对象,以便可以将顶级对象保存到NSUserDefaults中。

Here is the structure of objects: 这是对象的结构:

'Instructor' class '讲师'课

  • NSMutableArray that holds instances of... NSMutableArray包含...的实例

'Class' class '班级'班级

  • NSMutableArray that holds instances of... NSMutableArray包含...的实例

'Student' class 学生班

  • Name Property 名称属性
  • Number Property 编号属性
  • Money Property 金钱财产

I am needing to save an instance of Instructor to NSUserDefaults or to documents for the app. 我需要将Instructor的实例保存到NSUserDefaults或该应用程序的文档中。 As you can see the Instructor object is holding an array that is then holding instances of a class. 如您所见,Instructor对象持有一个数组,该数组随后持有一个类的实例。 That class object is holding instances of students. 该课堂对象持有学生实例。

Is the NSCoding protocol recursive? NSCoding协议是递归的吗? What I mean by that is if I add the NSCoding protocol to each class, I could then save an Instructor object and it would recursively encode the contained objects? 我的意思是,如果我将NSCoding协议添加到每个类中,然后可以保存一个Instructor对象,然后它将对包含的对象进行递归编码?

Would it then work the same way while decoding? 然后在解码时是否会以相同的方式工作? I could just decode the one instructor class and it would recursively decode the objects contained because they also conform to the NSCoding protocol? 我可以只解码一个教师类,并且可以递归地解码其中包含的对象,因为它们也符合NSCoding协议?

How could I go about setting this up? 我该如何进行设置?

Yes, you can (and probably should) write your support for NSCoding to be recursive. 是的,您可以(并且应该应该)编写对NSCoding的支持以递归。 That's how NSCoding is supposed to work. 那就是NSCoding应该如何工作的。

When your implement encodeWithCoder, simply call 在实现encodeWithCoder时,只需调用

[coder encodeObject: aProperty forKey: @"propertyName"];

on all your object's properties, including it's container properties. 所有对象的属性,包括容器的属性。

Then make sure every object in your object's object graph also conforms to NSCoding. 然后确保对象的对象图中的每个对象也符合NSCoding。

For scalar properties, you can save the scalar value using NSCoder methods like encodeInt:forKey: 对于标量属性,可以使用NSCoder方法(例如encodeInt:forKey)保存标量值:

To save an object that conforms to NSCoding to user defaults, first convert it to NSData using the NSKeyedArchiver class method archivedDataWithRootObject, then save the resulting data into defaults. 要将符合NSCoding的对象保存为用户默认值,请首先使用NSKeyedArchiver类方法archivedDataWithRootObject将其转换为NSData,然后将结果数据保存为默认值。 Quite simple, really. 真的很简单。

NSCoding isn't magic so it will work 'recursively' if your implementation of the encoding and decoding methods tells it to be. NSCoding并不是魔术,所以如果您的编码和解码方法实现的话,它将“递归”工作。

Implement the NSCoding methods and pass the data to be encoded to the encoder. 实现NSCoding方法并将要编码的数据传递给编码器。 Implement the NSCoding methods in all of your custom classes so that when you encode the array all of the contents can be processed appropriately. 在所有自定义类中实现NSCoding方法,以便在对数组进行编码时可以适当地处理所有内容。

Be sure to call super if the classes superclass also implements NSCoding . 如果类超类也实现了NSCoding请确保调用super

eg 例如

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:self.arrayOfClasses forKey:@"arrayOfClasses"];
}

- (id)initWithCoder:(NSCoder *)decoder {
    self.arrayOfClasses = [decoder decodeObjectForKey:@"arrayOfClasses"];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM