Think Cocoa

An unfortunate surprise when saving NSAttributedString in Core Data

Introduction Saving an attributed string in Core Data isn’t a difficult job. While the NSAttributedString type it’s not listed in the entity attribute’s type list, you can: Set entity attribute’s type to Transformable. Create a subclass of NSSecureUnarchiveFromDataTransformer, return NSAttributedString.self in allowedTopLevelClasses method. Register the new subclass of NSSecureUnarchiveFromDataTransformer before Core Data stack is initialized. Go back to Core Data Model editor, and update Transformer field of the attribute, with the class name from the previous step.