1

I'd like to know how to serialize an NSAttributedString to consecutively being able to store it in a core data managedObjectContext. However, I found this answer but there is no further explanation in the thread. Can somebody explain (in a nutshell) how to manually encode the attributed string? It would be highly appreciated!

Many thanks!

EDIT:

This is what I do:

NSMutableData *renderCacheData = [[NSMutableData alloc] init];
    NSKeyedArchiver *renderCacheArchiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:renderCacheData];
    [renderCacheArchiver encodeObject:bSelf.attString forKey:@"aS"];

At the last line it fails, the output is as following:

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

Now as I understood the other Questions in here, this is due to CT* objects not being able to serialize themselves (my NSAttributedString attributes dict contains a CTFont and CTColorRef attributes. The only thing is - I've got no idea how to write a category or (unlikely?) subclass on those types, so they are able to serialize themselves.

Community
  • 1
  • 1
LaK
  • 99
  • 1
  • 7
  • Could you show an example of an NSAttributedString and what happens when you try to archive it? – matt Apr 14 '13 at 18:59
  • Look at the aforementioned answer. In particular: "The class NSAttributedString is also NSCoding compliant, but the attributes are not, that's the problem." – gaige Apr 14 '13 at 19:53
  • Yeah, but that's exactly what I want to accomplish. If you look at the link I provided in the question, the last comment to the answer states that the particular user solved the problem by manually writing code to serialize the attributes array, that's what I need to accomplish then, too... – LaK Apr 14 '13 at 20:10
  • 1
    @LaK Look at the accepted answer to the duplicate I referenced. It has a link to a post on Apple's Developer Forums. That post has code to do what you want. – rmaddy Apr 14 '13 at 21:09
  • @maddy Thanks so much! I think it could be helpful if you could put this in here as an answer for people having the same question – as this _really_ solves the problem. – LaK Apr 14 '13 at 22:07

0 Answers0