繁体   English   中英

不符合协议'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显示问题导航器。
  • 单击问题行前面的显示三角形。

暂无
暂无

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

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