我正在使用NSKeyedUnarchiver来取消归档对象并希望使用委托(NSKeyedUnarchiverDelegate),但我的代理不会被调用.归档和取消归档工作正常,但代理(unarchiver& unarchiverDidFinish)未被调用.有人可以帮忙吗?
我有以下实现:
class BlobHandler: NSObject,NSKeyedUnarchiverDelegate{ func load() -> MYOBJECTCLASS{ let data:NSData? = getBlob(); var mykeyedunarchiver:NSKeyedUnarchiver=NSKeyedUnarchiver(forReadingWithData: data!); mykeyedunarchiver.delegate = self; let temp=mykeyedunarchiver.decodeObjectForKey("rootobject") // No delegates are called if temp==nil { blobsexists=false; }else{ objectreturn = temp! as! MYOBJECTCLASS; return objectreturn; } } func save1(myobject:MYOBJECTCLASS){ let data = NSMutableData() var keyedarchiver:NSKeyedArchiver=NSKeyedArchiver(forWritingWithMutableData: data); keyedarchiver.encodeObject(maptheme,forKey: "rootobject"); let bytes = data.bytes; let len=data.length; saveblob(bytes); }
从未调用以下代理,这些代理也在我的Blobhandler中实现:
func unarchiver(unarchiver: NSKeyedUnarchiver,cannotDecodeObjectOfClassName name: String,originalClasses classNames: [String]) -> AnyClass? { print("I am in unarchiver !"); return nil; } func unarchiverDidFinish(_ unarchiver: NSKeyedUnarchiver){ print("I am in unarchiverDidFinish ! "); }
我不知道它是什么,但它在清理和重建项目后工作. 我注意到不同的情况,有时构建不同步.有时代码在XCode中,但不执行.听起来令人难以置信,但我想这是真的. XCode 7.2