Search code examples
iphoneobjective-ccore-datansattributedstring

Storing NSAttributedString Core Data


I am trying to store an NSAttributedString to a Core Data SQL store.

I have the property set as a "transformable", it is optional and it is NOT transient or indexed and the value transformer name is set to default "NSKeyedUnarchiveFromData". In the .xcdatamodel and generated the managed object class which has this in the .h:

@property (nonatomic, retain) id Text; (I have tried changing id to NSAttributedString *Text)

and this in the .m:

@dynamic Text;

I look through and set the ".text" property of my NSManagedObject to the attributed string then when completed I do:

NSError *error = nil;
[managedObjectContext save:&error];

This through is causing this error in the output:

[NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0xc04edb0 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0xc04edb0'

I have checked the class of what I am storing to the property and it is NSAttributedString also I check responsesToSelector @selector(:) and this returns true so very confused as this is contrary to the error message?

Please advise.

Thanks James


Solution

  • I was checking the Apple Developer Forums and found a thread almost exactly the same as this question, one person had done this but unfortunately did not share the code. All they said was the following:

    "In Core Data i have an transformable in the database and i us my own NSVauleTransformer. This is a subclass of NSValueTransformer and creates an attributed string from the data object and back.

    Therefore i created a class called PersistableAttributedString which is NSCoding compliant. This class has a string and an array of attributes and builds the attributed string. I also created a class for the possible text attributes which is NSCoding compliant. Together the string and the attributes are all NSCoding compliant.

    The class NSAttributedString is also NSCoding compliant, but the attributes are not, that's the problem."

    Hope that might help.