简体   繁体   English

不符合协议'NSCoding' - Swift 3

[英]Does not conform to protocol 'NSCoding' - Swift 3

I have seen several questions similar to mine; 我见过几个类似于我的问题; however, those are pertaining to swift 2/1 and I am currently using swift 3. I believe Apple has changed it slightly. 然而,那些与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")
    }

}

You will notice how Swift 3 now forces me to use required convenience init( instead of required init( . Perhaps that has something to do with it. 您将注意到Swift 3现在如何强迫我使用required convenience init(而不是required init( 。也许这与它有关)。

How can I resolve this issue? 我该如何解决这个问题? Thanks! 谢谢!

The encode method in Swift 3 has been renamed to Swift 3中的encode方法已重命名为

func encode(with aCoder: NSCoder) 

When you get the do not conform error you can easily find out which required methods are missing 当您收到不符合的错误时,您可以轻松找出缺少哪些必需的方法

  • Press ⌘B to build the code. ⌘B构建代码。
  • Press ⌘4 to show the issue navigator. ⌘4显示问题导航器。
  • Click on the disclosure triangle in front of the issue line. 单击问题行前面的显示三角形。

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

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