不符合协议'NSCoding'

编程入门 行业动态 更新时间:2024-10-14 18:22:28
本文介绍了不符合协议'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'

本文发布于:2023-11-26 08:44:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633424.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不符合   协议   NSCoding

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!