简体   繁体   English

[iPhone]目标C,不符合NSCoding的对象。 如何将它们写入文件

[英][iPhone]Objective C, objects which do not conform to NSCoding. How to write them to a file

I am using an Objective c class, a subclass of NSObject. 我正在使用Objective c类,它是NSObject的子类。 This class cannot be modified. 此类无法修改。 I have an instance of this class that I wish to write to a file which can be retrieved and later reinstate. 我有一个此类的实例,希望将其写入可以检索并稍后恢复的文件中。 The object does not conform to NSCoding. 该对象不符合NSCoding。

To sum up, I need to save an instance of a class to a file which can be retrieved later, without using any of the NSCoding methods such as NSKeyedArchiving encodeWithCoder ... 总而言之,我需要将一个类的实例保存到一个文件中,以便以后使用,而无需使用任何NSCoding方法,例如NSKeyedArchiving encodeWithCoder ...

Using them returns this... NSInvalidArgumentException ...encodeWithCoder:] unrecognised selector sent to instance... 使用它们返回此... NSInvalidArgumentException ... encodeWithCoder:]无法识别的选择器已发送到实例...

Is there any other way I can store this object for later use Thank you 还有什么其他方法可以存储该对象供以后使用谢谢

ps The class is a UIEvent ps该类是一个UIEvent

您可以将其子类化并在子类中实现NSCoding协议吗?

Depending on what the object has as members, you could create a dictionary, and set each member's value as a key in the dictionary and then write that to file. 根据对象作为成员的方式,您可以创建一个字典,并将每个成员的值设置为字典中的键,然后将其写入文件。

However, I don't understand why you can't subclass the object and implement the NSCoding protocol, as Jaanus suggested? 但是,我不明白为什么您不能像Jaanus建议的那样对对象进行子类化并实现NSCoding协议?

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

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