不符合协议'NSCoding' - Swift 3 [英] Does not conform to protocol 'NSCoding' - Swift 3

查看:345
本文介绍了不符合协议'NSCoding' - Swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过几个类似我的问题;然而,这些都是关于快速2/1,我目前正在使用swift 3.我相信苹果已经稍微改变了。

  class Person:NSObject,NSCoding {

var signature:UIImage

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

需要方便的init(coder aDecoder:NSCoder){
let signature = aDecoder.decodeObject(forKey:signature)as! UIImage
self.init(签名:签名)
}

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

}

你会注意到Swift 3如何迫使我使用所需的便利性init(而不是必需的init(或许这与它有关系) / p>

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

解决方案

code> encode 在Swift 3中的方法已被重命名为

  func encode(with aCoder :NSCoder)

当您获得不符合错误时,您可以轻松地找出哪些需要的方法缺少




  • ⌘B构建代码。

  • ⌘4显示问题导航员。

  • 点击问题行前面的公开三角形。


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.

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.

How can I resolve this issue? Thanks!

解决方案

The encode method in Swift 3 has been renamed to

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.
  • Press ⌘4 to show the issue navigator.
  • Click on the disclosure triangle in front of the issue line.

这篇关于不符合协议'NSCoding' - Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆