Objective c 将NSMutableAttributedString写入/读取到Plist
好的,我们到了:Objective c 将NSMutableAttributedString写入/读取到Plist,objective-c,cocoa,rtf,nstextview,nsattributedstring,Objective C,Cocoa,Rtf,Nstextview,Nsattributedstring,好的,我们到了: 我有一个NSTextView 我得到了它的nsmutableAttributeString内容 我不能读/写给plist 通过使用Rob的代码(),我取得了一些进展(我正在设法将数据写入磁盘),但我无法恢复它(NSKeyedUnarchiver返回nil) 编码: // where MAS --> NSMutableAttributedString NSData* stringData = [NSKeyedArchiver archivedDataWithRoot
- 我有一个
NSTextView
- 我得到了它的
内容nsmutableAttributeString
- 我不能读/写给plist
NSKeyedUnarchiver
返回nil
)
编码:
// where MAS --> NSMutableAttributedString
NSData* stringData = [NSKeyedArchiver archivedDataWithRootObject:MAS];
NSMutableAttributedString* mas = (NSMutableAttributedString*)[NSKeyedUnarchiver unarchiveObjectWithData:dat];
NSData* j = [(NSMutableAttributedString*)MAS RTFDFromRange:NSMakeRange(0,[[MAS string] length])
documentAttributes:nil];
解码:
// where MAS --> NSMutableAttributedString
NSData* stringData = [NSKeyedArchiver archivedDataWithRootObject:MAS];
NSMutableAttributedString* mas = (NSMutableAttributedString*)[NSKeyedUnarchiver unarchiveObjectWithData:dat];
NSData* j = [(NSMutableAttributedString*)MAS RTFDFromRange:NSMakeRange(0,[[MAS string] length])
documentAttributes:nil];
有什么想法吗?欢迎任何可能的解决方法(即使不使用
NSCoder
,我怀疑它是否适用于RTFs…) 这是如何解决的
NSAttributedString
->NSData
:
// where MAS --> NSMutableAttributedString
NSData* stringData = [NSKeyedArchiver archivedDataWithRootObject:MAS];
NSMutableAttributedString* mas = (NSMutableAttributedString*)[NSKeyedUnarchiver unarchiveObjectWithData:dat];
NSData* j = [(NSMutableAttributedString*)MAS RTFDFromRange:NSMakeRange(0,[[MAS string] length])
documentAttributes:nil];
NSData
->NSAttributedString
NSMutableAttributedString* mas = [[NSMutableAttributedString alloc] initWithRTFD:dat
documentAttributes:nil];
就这么简单。注意,取消归档/反序列化对象通常会导致不可变副本。您能否仅使用NSData的WriteFile:原子方式:写入它,然后使用dataWithContentsOfFile将其读回?@标记好,我会;如何将我的
NSData
转换回NSAttributedString
?啊,我明白你的意思了。让我想想。@mark不幸的是,这不是储蓄本身的问题;但对于归档/取消归档和反序列化:/