swift – 如何调用validateValue方法
发布时间:2020-12-14 04:55:29 所属栏目:百科 来源:网络整理
导读:我正在尝试制作通用的NSCoding实现,并且由于更新版本的应用程序,当对象的类型在平均时间内发生变化时会出现解码问题. 我从Swift调用validateValue方法时遇到问题.功能签名是: func validateValue(_ ioValue: AutoreleasingUnsafeMutablePointerAnyObject?,f
我正在尝试制作通用的NSCoding实现,并且由于更新版本的应用程序,当对象的类型在平均时间内发生变化时会出现解码问题.
我从Swift调用validateValue方法时遇到问题.功能签名是: func validateValue(_ ioValue: AutoreleasingUnsafeMutablePointer<AnyObject?>,forKey key: String,error outError: NSErrorPointer) -> Bool 作为参考,可以在此处找到Apple文档:NSCoding validateValue 我想要创建的代码是: public class func decodeObjectWithCoder(theObject:NSObject,aDecoder: NSCoder) { for (key,value) in toDictionary(theObject) { if aDecoder.containsValueForKey(key) { let newValue: AnyObject? = aDecoder.decodeObjectForKey(key) NSLog("set key (key) with value (newValue)") var error:NSError? var y:Bool = theObject.validateValue(newValue,forKey: key,error: &error) if y { theObject.setValue(newValue,forKey: key) } } } } 我无法正确调用.validateValue.我不断收到编译错误.我该怎么称呼它. 更新:我刚刚发现这段代码编译: var ioValue: AutoreleasingUnsafeMutablePointer<AnyObject?> = nil var y:Bool = theObject.validateValue(ioValue,error: nil) 这意味着AnyObject类型的值?无法转换为AutoreleasingUnsafeMutablePointer 解决方法
必须通过添加&来将newValue字段作为指针传递.在它面前.除此之外,你必须使用var而不是let.所以最终的代码是:
public class func decodeObjectWithCoder(theObject:NSObject,aDecoder: NSCoder) { for (key,value) in toDictionary(theObject) { if aDecoder.containsValueForKey(key) { var newValue: AnyObject? = aDecoder.decodeObjectForKey(key) if theObject.validateValue(&newValue,error: nil) { theObject.setValue(newValue,forKey: key) } } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |