Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 将NSMutableAttributedString写入/读取到Plist_Objective C_Cocoa_Rtf_Nstextview_Nsattributedstring - Fatal编程技术网

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
通过使用Rob的代码(),我取得了一些进展(我正在设法将数据写入磁盘),但我无法恢复它(
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不幸的是,这不是储蓄本身的问题;但对于归档/取消归档和反序列化:/