Id welcome any help unarchiving an NSArray of UIBezierPaths from NSData. I want to use the NSArray container to hold some UIBezierPaths which I can persist to Core Data as an NSData property in my entity (rather than persist them individually, which Ive done elsewhere in this project).
- I am able to archive and unarchive a single UIBezierPath without putting it in an array (example 1 below)
- Im also able to archive and unarchive an array of NSStrings in the NSArray (example 2 below) (just to illustrate that I can archive and unarchive using an array container)
However,
- When I put a UIBezierPath in an NSArray, Im getting UNARCHIVED Object = (null) CLASS: (null) in the console. (example 3 below) Ive put some of the console output from this example at the bottom of this question.
I feel I should be using [NSKeyedUnarchiver unarchivedObjectOfClass:fromData:error:] (rather than the deprecated methods) and Ive searched all the posts I can find on [NSKeyedUnarchiver unarchivedObjectOfClass:] but I haven't been able to work it out.
Im pretty stuck on this and I feel Im missing something basic, so any steer is welcome
Many thanks
//1. WORKS ===================
UIBezierPath* p = [_pathManager.Paths objectAtIndex:1];
NSLog(@"Path: %@", p);
NSError* error = nil;
NSData *obj = [NSKeyedArchiver archivedDataWithRootObject:p requiringSecureCoding:YES error:&error];
NSLog(@" NS DATA: %@", obj);
NSError *error1;
UIBezierPath* arr = [NSKeyedUnarchiver unarchivedObjectOfClass:[UIBezierPath class] fromData:obj error:&error1];
NSLog(@" UNARCHIVED Object = %@ CLASS: %@", arr, [arr class] );
//2. ALSO WORKS - archiving and unarchiving NSStrings in an NSArray
NSError* error = nil;
NSArray* arr1 = [[NSArray alloc]initWithObjects:@"Test1",@"Test2",nil];
NSLog(@" ARRAY: %@", arr1);
NSData *obj = [NSKeyedArchiver archivedDataWithRootObject:arr1 requiringSecureCoding:YES error:&error];
NSLog(@" NS DATA: %@", obj);
NSError *error1;
NSArray* arr = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSArray class] fromData:obj error:&error1];
NSLog(@" UNARCHIVED Object = %@ CLASS: %@", arr, [arr class] );
//3. DOESNT WORK - console returns UNARCHIVED Object = (null) CLASS: (null)
UIBezierPath* p = [_pathManager.Paths objectAtIndex:1];
NSLog(@"Path: %@", p);
NSError* error = nil;
NSArray* arr1 = [[NSArray alloc]initWithObjects: p , nil];
NSLog(@" ARRAY: %@", arr1);
NSData *obj = [NSKeyedArchiver archivedDataWithRootObject:arr1 requiringSecureCoding:YES error:&error];
NSLog(@" NS DATA: %@", obj);
NSError *error1;
NSArray* arr = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSArray class] fromData:obj error:&error1];
NSLog(@" UNARCHIVED Object = %@ CLASS: %@", arr, [arr class] );
Sample Console output below:
2020-06-13 12:27:29.236477+0100 PROJ[24718:931062] Path: <UIBezierPath: 0x60000560de60; <MoveTo {6321, 4648}>,
<MoveTo {6334.5377384528101, 4651.0555672716337}>,
<CurveTo {6338.0387705020785, 4653.1006231477277} {6335.121243794355, 4651.3964099176492} {6336.862553352169, 4652.4247805556097}>,
<CurveTo {6341.5950413522669, 4655.1106228243434} {6339.2149876519879, 4653.7764657398457} {6340.4053789767258, 4654.4454254015836}>,
..... etc....
<CurveTo {6427.7455441302509, 4702.2045296954602} {6425.1616837301008, 4701.18468119315} {6427.1089369371202, 4701.9410553993894}>,
<LineTo {6446.3401401523624, 4712.478597704282}>
2020-06-13 12:27:29.247213+0100 PROJ[24718:931062] ARRAY: (
"<UIBezierPath: 0x60000560de60; <MoveTo {6321, 4648}>,\n <MoveTo {6334.5377384528101, 4651.0555672716337}>,\n <CurveTo {6338.0387705020785, 4653.1006231477277} {6335.121243794355, 4651.3964099176492} {6336.862553352169, 4652.4247805556097}>,\n <CurveTo {6341.5950413522669, 4655.1106228243434} {6339.2149876519879, 4653.7764657398457} {6340.4053789767258, 4654.4454254015836}>,\n
..... etc....
<CurveTo {6427.7455441302509, 4702.2045296954602} {6425.1616837301008, 4701.18468119315} {6427.1089369371202, 4701.9410553993894}>,\n <LineTo {6446.3401401523624, 4712.478597704282}>"
)
2020-06-13 12:27:29.247942+0100 PROJ[24718:931062] NS DATA: {length = 1614, bytes = 0x62706c69 73743030 d4010203 04050607 ... 00000000 000005c0 }
2020-06-13 12:27:29.248213+0100 PROJ[24718:931062] UNARCHIVED Object = (null) CLASS: (null)