不符合协议 'NSCoding' - Swift 3

Does not conform to protocol 'NSCoding' - Swift 3

我见过几个和我类似的问题;但是,这些与 swift 2/1 有关,我目前使用的是 swift 3。我相信 Apple 对其进行了轻微更改。

class Person: NSObject, NSCoding {

    var signature: UIImage

    init(signature: UIImage) {
        self.signature = signature
    }

    required convenience init(coder aDecoder: NSCoder) {
        let signature = aDecoder.decodeObject(forKey: "signature") as! UIImage
        self.init(signature: signature)
    }

    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encode(signature, forKey: "signature")
    }

}

您会注意到 Swift 3 现在如何迫使我使用 required convenience init( 而不是 required init(。可能跟这个有关系。

我该如何解决这个问题?谢谢!

Swift 3 中的 encode 方法已重命名为

func encode(with aCoder: NSCoder) 

当您收到 不符合 错误时,您可以轻松找出缺少哪些必需方法

  • ⌘B 构建代码。
  • ⌘4 显示问题导航器。
  • 点击问题线前面的披露三角形。