简体   繁体   English

使用NSCoder编码数组

[英]Encode Array with NSCoder

I have a User Class that uses a saveuser() method whenever the application terminates. 我有一个用户Classsaveuser()在应用程序终止时使用saveuser()方法。 The User has two Arrays of Custom Classes that sub-class NSObject . 用户有两个自定义Classes Arrays ,它们是NSObject子类。 Here is my encode method. 这是我的编码方法。

func encode(with aCoder: NSCoder) {
    aCoder.encode(self.firstName, forKey: coderKey.fName)
    aCoder.encode(self.lastName, forKey: coderKey.lName)
    aCoder.encode(self.bio, forKey: coderKey.bio)
    aCoder.encode(self.tags, forKey: coderKey.tags)
    aCoder.encode(self.organizations, forKey: coderKey.orgs)
    aCoder.encode(self.img, forKey: coderKey.img)
}

The app Crashes when encoding self.tags . 编码self.tags时,应用程序崩溃。 I assume it will do the same with self.organizations because it is also an array of NSObjects and possibly with self.img because it is a UIImage . 我认为它将对self.organizations进行相同的操作,因为它也是一个NSObjects array ,可能self.img因为它是UIImage Here is the error I am getting. 这是我遇到的错误。

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Social_Justice.Tag encodeWithCoder:]: unrecognized selector sent to instance 0x60000005efc0' 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[[Social_Justice.Tag encodeWithCoder:]:无法识别的选择器已发送到实例0x60000005efc0'

What should I do to resolve this issue? 我应该怎么做才能解决这个问题? If you need to see any more code, just comment and i'll edit. 如果您需要查看更多代码,请注释,然后编辑。

as David Berry commented on the original post. 正如David Berry在原始帖子上发表的评论一样。 You have to Make sure any custom classes that you are trying to encode, also conform to NSCoder. 您必须确保要尝试编码的所有自定义类也要符合NSCoder。 They don't need to have archive paths, they just need to have the encode and decode functions. 他们不需要存档路径,只需要具有编码和解码功能。

Answer: 回答:

  1. I assume the class name of the object inside the array self.tag s and self.organization s are Tag and Organization 我假设数组self.tag S和self.organization S的内部对象的类名是标记组织

  2. Objective-C use a very different function calling style. Objective-C使用非常不同的函数调用样式。 If you see [ClassName/ObjectName methodName] which is somehow equivalent to ObjectName.function() style in Swift, let's put it in this way for the moment (the Swift compiler will be better in the future, and hopefully you will not see objctive-C warnings anymore when debugging) 如果您看到[ClassName/ObjectName methodName]在某种程度上等效于Swift中的ObjectName.function()样式,那么让我们暂时以这种方式进行介绍(Swift编译器将来会更好,并且希望您不会看到目标) -C警告在调试时不再存在)

  3. A selector is a term used by Objective-C, you could think this is a method. 选择器是Objective-C使用的术语,您可能会认为这是一种方法。 Therefore this unrecognized selector error warning tells you the code tried to call a method named encodeWithCode() that doesn't actually exist inside the object Tag and Organization (the NSCoder system knows how to encode an array, but has no idea to encode your own object inside Array) 因此,此无法识别的选择器错误警告会告诉您代码尝试调用名为encodeWithCode()的方法,该方法实际上并不存在于对象TagOrganization中 (NSCoder系统知道如何对数组进行编码,但不知道对自己的数组进行编码)数组中的对象)

  4. If you want to use the 5 concrete classes of the NSCoder system on an object, the object MUST conform the NSCoding protocol. 如果要在对象上使用NSCoder系统的5个具体类,则该对象务必符合NSCoding协议。 It means the class ( Tag and Organization ) must implement init?(coder: NSCoder) and func encode(with: NSCoder) 这意味着该类( TagOrganization )必须实现init?(coder: NSCoder)func encode(with: NSCoder)

  5. The 4 concrete class of NSCoder are NSArchiver, NSUnarchiver, NSKeyedArchiver, NSKeyedUnarchiver, and NSPortCoder. NSCoder的4个具体类是NSArchiver, NSUnarchiver, NSKeyedArchiver, NSKeyedUnarchiver, and NSPortCoder.

Further Reading: 进一步阅读:

NSCoding / NSKeyed​Archiver By NSHipster NSCoding / NSKeyedArchiver通过NSHipster

Or: (my favorite) 或:(我的最爱)

Apple Document NSCoder 苹果文档NSCoder

Apple Document NSKeyedArchiver 苹果文档NSKeyedArchiver

Apple Document NSCoding 苹果文档NSCoding

Or: (If those documents are still a bit confuse for you) 或:(如果这些文件仍然让您感到困惑)

Hollemans M. 2016, iOS Apprentice Fifth Edition Tutorial 2 Checklist, pp 126 ~ pp 137 Hollemans M., 2016年iOS Apprentice第五版教程2清单,pp 126〜pp 137

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

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