使NSObject类可以序列化。

时间:2022-09-24 22:51:15

I want a custom class based on NSObject to be serializable. I have read this but want to ask some additional Qs.

我希望基于NSObject的自定义类是可序列化的。我已经读过这篇文章了,但我想再问几个问题。

  1. Does NSObject implement NSCoder?
  2. NSObject实现NSCoder吗?
  3. I also want to ask if I can serialize an array of some objects of this custom class that I want to be serializable. Does this require some special init?
  4. 我还想问,我是否可以序列化这个自定义类的一些对象的数组,我希望它是可序列化的。这需要特殊的init吗?
  5. I know that I should implement initWithCoder and encodeWithCoder. Is it the variables in my custom object class that I should Encode/Decode there?
  6. 我知道我应该实现initWithCoder和encodeWithCoder。在我的自定义对象类中,我应该对那里进行编码/解码吗?

1 个解决方案

#1


2  

  • NSObject doesn't adopt the protocol NSCoding. NSCoder is a class, and NSObject is the class from which all the other Cocoa classes are derived.
  • NSObject不采用协议NSCoding。NSCoder是一个类,NSObject是派生其他Cocoa类的类。
  • Your class doesn't need to do anything special, in that case; If the array is an NSArray, or an NSMutableArray, then the array will take care of the objects it contains.
  • 在这种情况下,你的类不需要做任何特殊的事情;如果数组是NSArray或NSMutableArray,那么数组将负责它包含的对象。
  • Yes. For more details see Archives and Serializations Programming Guide: Introduction.
  • 是的。有关更多细节,请参阅档案和序列化编程指南:简介。

#1


2  

  • NSObject doesn't adopt the protocol NSCoding. NSCoder is a class, and NSObject is the class from which all the other Cocoa classes are derived.
  • NSObject不采用协议NSCoding。NSCoder是一个类,NSObject是派生其他Cocoa类的类。
  • Your class doesn't need to do anything special, in that case; If the array is an NSArray, or an NSMutableArray, then the array will take care of the objects it contains.
  • 在这种情况下,你的类不需要做任何特殊的事情;如果数组是NSArray或NSMutableArray,那么数组将负责它包含的对象。
  • Yes. For more details see Archives and Serializations Programming Guide: Introduction.
  • 是的。有关更多细节,请参阅档案和序列化编程指南:简介。