Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write/Read NSMutableAttributedString to Plist

OK, here we are :

  • I've got an NSTextView
  • I'm getting it's NSMutableAttributedString content
  • I'm unable to read/write it to plist

By using Rob's code ( Saving custom attributes in NSAttributedString ), I've made some progress (I'm managing to write the data to disk), but I cannot recover it (= NSKeyedUnarchiver returns nil).


Encoding :

// where MAS --> NSMutableAttributedString

NSData* stringData = [NSKeyedArchiver archivedDataWithRootObject:MAS];

Decoding :

NSMutableAttributedString* mas = (NSMutableAttributedString*)[NSKeyedUnarchiver unarchiveObjectWithData:dat];

Any ideas? Any possible workaround (even if not with NSCoder, which I doubt it works with RTFs...) would be welcome!

like image 832
Dr.Kameleon Avatar asked Apr 18 '26 11:04

Dr.Kameleon


1 Answers

And here's how it was solved.

NSAttributedString -> NSData :

NSData* j = [(NSMutableAttributedString*)MAS RTFDFromRange:NSMakeRange(0,[[MAS string] length]) 
                                        documentAttributes:nil];

NSData -> NSAttributedString

NSMutableAttributedString* mas = [[NSMutableAttributedString alloc] initWithRTFD:dat 
                                                              documentAttributes:nil];

Simple as that.

like image 139
Dr.Kameleon Avatar answered Apr 20 '26 13:04

Dr.Kameleon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!