大佬教程收集整理的这篇文章主要介绍了ios – Swift:不符合协议NSCoding,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
class ServerInfo: NSObject,NSCoding { var username = "" var password = "" var domain = "" var LOCATIOn = "" var serverFQDN = "" var serverID = "" override init() { } init(coder aDecoder: NSCoder!) { self.username = aDecoder.decodeObjectForKey("username") as NSString self.password = aDecoder.decodeObjectForKey("password") as NSString self.domain = aDecoder.decodeObjectForKey("domain") as NSString self.LOCATIOn = aDecoder.decodeObjectForKey("LOCATIOn") as NSString self.serverFQDN = aDecoder.decodeObjectForKey("serverFQDN") as NSString self.serverID = aDecoder.decodeObjectForKey("serverID") as NSString } func encodeWithCoder(_aCoder: NSCoder!) { _aCoder.encodeObject(self.username,forKey: "username") _aCoder.encodeObject(self.password,forKey: "password") _aCoder.encodeObject(self.domain,forKey: "domain") _aCoder.encodeObject(self.LOCATIOn,forKey: "LOCATIOn") _aCoder.encodeObject(self.serverFQDN,forKey: "serverFQDN") _aCoder.encodeObject(self.serverID,forKey: "serverID") } }
这是一个bug还是我只是想念一些东西?
error: type 'ServerInfo' does not conform to protocol 'NSCoding' class ServerInfo: NSObject,NSCoding { ^ Foundation.NSCoding:2:32: note: protocol requires function 'encodeWithCoder' with type '(NSCoder) -> Void' @objc(encodeWithCoder:) func encodeWithCoder(aCoder: NSCoder) ^ note: candidate has non-matching type '(NSCoder!) -> ()' func encodeWithCoder(_aCoder: NSCoder!) { ^ Foundation.NSCoding:3:25: note: protocol requires initializer 'init(coder:)' with type '(coder: NSCoder)' @objc(initWithCoder:) init(coder aDecoder: NSCoder) ^ note: candidate has non-matching type '(coder: NSCoder!)' init(coder aDecoder: NSCoder!) {
(这可能在beta版本之间发生变化)
此外,initWithCoder方法必须标记为必需:
required init(coder aDecoder: NSCoder) { } func encodeWithCoder(_aCoder: NSCoder) { }
在Swift 3中,所需的方法是
required init(coder aDecoder: NSCoder) { } func encode(with aCoder: NSCoder) { }
以上是大佬教程为你收集整理的ios – Swift:不符合协议NSCoding全部内容,希望文章能够帮你解决ios – Swift:不符合协议NSCoding所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。