且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在自定义类上使用NSCoding时在self.init调用错误之前使用的'self'

更新时间:2023-10-22 08:18:22

该错误消息具有误导性,但是您需要将init(coder:)设置为指定的初始化程序.

The error message is sort of misleading, but you need to make init(coder:) a designated initializer.

您需要将init(code:)修改为以下内容:

You need to modify your init(code:) to something like this:

required init?(coder aDecoder: NSCoder) {
    guard let unarchivedId = aDecoder.decodeObjectForKey("id") as? Int else {
            return nil
    }
    self.id = unarchivedId
    super.init()
}