Post not yet marked as solved
Post marked as unsolved with 3 replies, 1,361 views
I have an application that uses a Transformable property in Core Data to store NSAttributedStrings and get compiler warnings
Object.property is using a nil or insecure value transformer. Please switch to NSSecureUnarchiveFromDataTransformerName or a custom NSValueTransformer subclass of NSSecureUnarchiveFromDataTransformer [2]
NSSecureUnarchiveFromDataTransformerName does not support archiving and unarchiving of NSAttributedStrings and so as I understand it I have to create a custom transformer, register that in AppDelegate and enter the transformer class name in the Core Data models object property details.
Below is the custom transformer class, however I get an error when trying to decode existing attributed strings. Can anyone shed any light on this ? Why is the new unarchiver unable to handle the NSFileWrapper given this is a property of the NSTextAttachment and works fine with the deprecated unarchiver ?
Is this a bug or intentional ?
Is there some way to add support for unarchiving the NSFileWrapper ?
@implementation NSAttributedStringValueTransformer(Class)transformedValueClass {
return [NSAttributedString class];
}(void)initialize {
[NSValueTransformer setValueTransformer:[[self alloc]init] forName:@"NSAttributedStringValueTransformer"];
}
(BOOL)allowsReverseTransformation {
return YES;
}
(NSData*)transformedValue:(NSAttributedString*)value {
NSError *error;
NSData* stringAsData = [NSKeyedArchiver archivedDataWithRootObject:value requiringSecureCoding:false error:&error];
if (error != nil) {
NSLog(@"Error encoding attributed string: %@", error.localizedDescription);
return nil;
}
return stringAsData;
}
(NSAttributedString*)reverseTransformedValue:(NSData*)value {
NSError *error;
/* This works */
/* NSAttributedString* string = [NSKeyedUnarchiver unarchiveObjectWithData: value]; /
/* This fails with the error The data couldn’t be read because it isn’t in the correct format. */
NSAttributedString* string = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSAttributedString class] fromData:value error:&error];
if (error != nil) {
NSLog(@"Error decoding attributed string: %@", error);
return nil;
}
return string;
}
@end
Resulting Error:
Error decoding attributed string:
[Error](https://developer.apple.com/forums/content/attachment/547d2a08-9220-42aa-9d29-1b3129feb864){: .log-attachment}
Error Domain=NSCocoaErrorDomain Code=4864 "value for key 'NSFileWrapper' was of unexpected class 'NSFileWrapper (0x1c9d40d48) [/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/Foundation.framework]'. Allowed classes are '{(
"NSURL (0x1c9d1b988) [/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/CoreFoundation.framework]",
"NSAttributedString (0x1c9d36668) [/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/Foundation.framework]",
"NSFont (0x1c9e1a3c8) [/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIFoundation.framework]",
"NSDictionary (0x1c9d1adf8) [/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/CoreFoundation.framework]",
"NSArray (0x1c9d1ab28) [/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/CoreFoundation.framework]",
"NSColor (0x1c9ec2198) [/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIKitCore.framework]",
"NSTextAttachment (0x1c9e1aad0) [/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIFoundation.framework]",
"NSGlyphInfo (0x1c9e198d8) [/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIFoundation.framework]",
...