如何实现unarchiver代理覆盖类名? [英] How to implement the unarchiver delegate for overwriting the classname?
问题描述
在MYAPPTARGET_2中从BLOB取消归档流程时,我收到错误:
016-01-18 15:01:38.541 WorldHistoryAtlasTest [598: 9405] *由于未捕获异常终止应用程序NSInvalidUnarchiveOperationException,
reason:'* - [NSKeyedUnarchiver decodeObjectForKey:]:无法解码类
(MYAPPTARGET_1.MapTheme)的对象键(rootobject);
所以看起来好像是用MYAPPTARGET_1的前缀编码,并且从MYAPPTARGET_2可以读取。
所以我得到了一个提示,在另一个STACKOVERFLOW答案中克服这一点,使用委托unarchiver覆盖Classname。但是我无法实现这一点:
func unarchiver(unarchiver:NSKeyedUnarchiver,cannotDecodeObjectOfClassName name:String,originalClasses classNames:[String ]) - > AnyClass? {
print(ClassName is+ name);
return nil; //做什么,覆盖类别名称。
}
var mykeyedunarchiver:NSKeyedUnarchiver = NSKeyedUnarchiver(forReadingWithData:data!);
mykeyedunarchiver.delegate = self;
let temp = mykeyedunarchiver.decodeObjectForKey(rootobject)
我有一个带有目标名称的应用MYAPPTARGET_1和我使用NSKeyedArchiver功能将一些数据存储为blob。
然后,再有一个名为MYAPPTARGET_2的第二个Apps-Target,我尝试再次读取数据。从DB中存储的BLOB加载。
如果有人可以给我一个实际的提示,我会很高兴。
以下是足够的:
class MyObject:NSObject,NSCoding {...}
let my = MyObject()
// archive
NSKeyedArchiver.setClassName(MyObject,for: MyObject.self)
let data = NSKeyedArchiver.archivedData(withRootObject:r)
// unarchive
NSKeyedUnarchiver.setClass(MyObject.self,forClassName:MyObject)
let _ = NSKeyedUnarchiver.unarchiveObject(with:data)
这有助于我在一个mac应用程序和一个iphone应用程序
- I have 2 Targets of my IOS-App : MYAPPTARGET_1 & MYAPPTARGET_2
- MYAPPTARGET_1 is writing a Stream to a BLOB using NSKeyedArchiver
- MYAPPTARGET_2 is reading a Stream from a BLOB using NSKeyedUnarchiver
When unarchiving the Stream from the BLOB in my MYAPPTARGET_2, I get the Error: 016-01-18 15:01:38.541 WorldHistoryAtlasTest[598:9405] * Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '* -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (MYAPPTARGET_1.MapTheme) for key (rootobject);
So it seemes to be obviously encoded with a prefix of MYAPPTARGET_1 and is not readable from MYAPPTARGET_2.
So I got a hint for overcome this in another STACKOVERFLOW Answer to use a delegate unarchiver to overwrite the Classname. But I am not able to implement this:
func unarchiver(unarchiver: NSKeyedUnarchiver, cannotDecodeObjectOfClassName name: String, originalClasses classNames: [String]) -> AnyClass? {
print("ClassName is " + name);
return nil; // WHAT TO DO , TO OVERWRITE THE CLASS NAME.
}
var mykeyedunarchiver:NSKeyedUnarchiver=NSKeyedUnarchiver(forReadingWithData: data!);
mykeyedunarchiver.delegate = self;
let temp=mykeyedunarchiver.decodeObjectForKey("rootobject")
I have an App with targetname MYAPPTARGET_1 and I store some data as blob using the NSKeyedArchiver feature.
Then later with a second Apps-Target named MYAPPTARGET_2 I try to read the data again. Load from the BLOB stored in the DB.
I would be so happy if someone could give me a practical hint on this.
the following is enough:
class MyObject: NSObject, NSCoding {...}
let my = MyObject()
// archive
NSKeyedArchiver.setClassName("MyObject", for: MyObject.self)
let data = NSKeyedArchiver.archivedData(withRootObject: r)
// unarchive
NSKeyedUnarchiver.setClass(MyObject.self, forClassName: "MyObject")
let _ = NSKeyedUnarchiver.unarchiveObject(with: data)
this helped me to exchange custom objects between a mac-app and an iphone app
这篇关于如何实现unarchiver代理覆盖类名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!