我正在尝试制作通用的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.我不断收到编译错误.我该怎么称呼它.
toDictionary函数可在以下位置找到:EVReflection
更新:我刚刚发现这段代码编译:
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) } } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。