将NSMutableAttributedString写入/读取到Plist

Dr.*_*eon 3 cocoa rtf objective-c nstextview nsattributedstring

好的,我们在这里:

  • 我有一个 NSTextView
  • 我得到它的NSMutableAttributedString满足
  • 我无法将其读/写到plist

通过使用Rob的代码(将自定义属性保存在NSAttributedString中),我已经取得了一些进展(我正在设法将数据写入磁盘),但是我无法恢复它(= NSKeyedUnarchiverreturn nil)。


编码方式:

// where MAS --> NSMutableAttributedString

NSData* stringData = [NSKeyedArchiver archivedDataWithRootObject:MAS];
Run Code Online (Sandbox Code Playgroud)

解码:

NSMutableAttributedString* mas = (NSMutableAttributedString*)[NSKeyedUnarchiver unarchiveObjectWithData:dat];
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?任何可能的解决方法(即使不能使用NSCoder,我都怀疑它可以与RTF一起使用...)!

Dr.*_*eon 5

这就是解决方法。

NSAttributedString-> NSData

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

NSData -> NSAttributedString

NSMutableAttributedString* mas = [[NSMutableAttributedString alloc] initWithRTFD:dat 
                                                              documentAttributes:nil];
Run Code Online (Sandbox Code Playgroud)

就那么简单。