I am Serializing an Object in my APP_TARGET1 and store it (sqlite, file)
let data : NSData = NSKeyedArchiver.archivedDataWithRootObject(maptheme);
I am reading back the blob/file with APP_TARGET2 and try to deserialize the object.
let temp=NSKeyedUnarchiver.unarchiveObjectWithData(data!);
-> I get the following error, which indicates, that serialized objects are depending on bundli-ids ?
Please someone tell me, that this is not true and that there is a workaround to have an apps-target, which writes the obejcts and another target which reads the objects.
Any help ?
NSKeyedUnarchiver.unarchiveObjectWithData(data!);
2016-01-15 10:21:11.476 APP_TARGET1[1684:38753] *** Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (APP_TARGET2.MapTheme) for key (root); the class may be defined in source code or a library that is not linked'
*** First throw call stack:
(
0 CoreFoundation 0x000000010f5cce65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010f043deb objc_exception_throw + 48
2 Foundation 0x000000010ec8835e -[NSCoder(Exceptions) __failWithExceptionName:errorCode:format:] + 0
3 Foundation 0x000000010ec88572 -[NSCoder(Exceptions) __failWithExceptionName:errorCode:format:] + 532
4 Foundation 0x000000010ebe3927 _decodeObjectBinary + 1659
5 Foundation 0x000000010ebe31a5 _decodeObject + 281
6 Foundation 0x000000010ec05931 +[NSKeyedUnarchiver unarchiveObjectWithData:] + 89
7 WorldHistoryAtlasTest 0x000000010e748c11 _TF21WorldHistoryAtlasTest15loadThemeasBlobFSST6existsSb8mapThemeCS_8MapTheme_ + 3745
8 WorldHistoryAtlasTest 0x000000010e781d9f _TFC21WorldHistoryAtlasTest22TimeLineViewController18load_map_meta_datafS0_FT_T_ + 207
9 WorldHistoryAtlasTest 0x000000010e781231 _TFC21WorldHistoryAtlasTest22TimeLineViewController11viewDidLoadfS0_FT_T_ + 5889
10 WorldHistoryAtlasTest 0x000000010e781cb2 _TToFC21WorldHistoryAtlasTest22TimeLineViewController11viewDidLoadfS0_FT_T_ + 34
11 UIKit 0x00000001102f9f98 -[UIViewController loadViewIfRequired] + 1198
12 UIKit 0x00000001102fa2e7 -[UIViewController view] + 27
13 UIKit 0x0000000110aa4f87 -[_UIFullscreenPresentationController _setPresentedViewController:] + 87
14 UIKit 0x00000001102c9f62 -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 133
15 UIKit 0x000000011030cc8c -[UIViewController _presentViewController:withAnimationController:completion:] + 4002
16 UIKit 0x000000011030ff2c -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 489
17 UIKit 0x000000011030fa3b -[UIViewController presentViewController:animated:completion:] + 179
18 WorldHistoryAtlasTest 0x000000010e6ba862 _TFC21WorldHistoryAtlasTest14ViewController12showTimelinefS0_FT_T_ + 594
19 WorldHistoryAtlasTest 0x000000010e6b7ff2 _TFC21WorldHistoryAtlasTest14ViewController10loadModulefS0_FSST_ + 4002
20 WorldHistoryAtlasTest 0x000000010e6d7036 _TFFFC21WorldHistoryAtlasTest14ViewController10loadModuleFS0_FSST_U2_FT_T_U0_FT_T_ + 86
21 WorldHistoryAtlasTest 0x000000010e632d57 _TTRXFo__dT__XFdCb__dT__ + 39
22 libdispatch.dylib 0x0000000111d03e5d _dispatch_call_block_and_release + 12
23 libdispatch.dylib 0x0000000111d2449b _dispatch_client_callout + 8
24 libdispatch.dylib 0x0000000111d0c2af _dispatch_main_queue_callback_4CF + 1738
25 CoreFoundation 0x000000010f52cd09 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
26 CoreFoundation 0x000000010f4ee2c9 __CFRunLoopRun + 2073
27 CoreFoundation 0x000000010f4ed828 CFRunLoopRunSpecific + 488
28 GraphicsServices 0x0000000113f94ad2 GSEventRunModal + 161
29 UIKit 0x0000000110163610 UIApplicationMain + 171
30 WorldHistoryAtlasTest 0x000000010e7414fd main + 109
31 libdyld.dylib 0x0000000111d5992d start + 1
32 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
UPDATE
Try to implement the Solution:
class BlobHandler: NSObject , NSKeyedUnarchiverDelegate{
let data:NSData? = NSData(bytes: point, length: Int(len));
//NSKeyedUnarchiver.set
var mykeyedunarchiver:NSKeyedUnarchiver=NSKeyedUnarchiver(forReadingWithData: data!);
mykeyedunarchiver.delegate = self;
let temp=mykeyedunarchiver.decodeObject();
.... .... ....
func unarchiver(_ unarchiver: NSKeyedUnarchiver, cannotDecodeObjectOfClassName name: String, originalClasses classNames: [String]) -> AnyClass? {
print("I am in the delegated method !");
return nil;
}
The problem is not the bundle id, the problem is that the classes compile differently because they are built into different targets.
You can deal with this by adding a delegate to the keyed unarchiver and implementing:
optional func unarchiver(_ unarchiver: NSKeyedUnarchiver, cannotDecodeObjectOfClassName name: String, originalClasses classNames: [String]) -> AnyClass?
so that you can check the requested class name and return the appropriate matching class in the current target.