我正在尝试将 XML 数据保存并检索到文件中。
为此,我使用 NSDictionary
的 writeToFile:
问题是,如何使用 NSDictionary
的 writeToFile:
在磁盘上的文件中写入和检索属性字符串?
最佳答案
如果您需要比归档对象更可移植的东西,请考虑使用 NSAttributedString 的 RTF 表示形式:
NSAttributedString* attrString = [[NSAttributedString alloc] initWithString:@"Attributed String"
attributes:@{ NSForegroundColorAttributeName: [NSColor redColor]}];
NSData* rtfData = [attrString RTFFromRange:NSMakeRange(0, attrString.length) documentAttributes:nil];
[rtfData writeToFile:@"string.rtf" atomically:YES];
您可以读回来:
NSData* rtfData = [NSData dataWithContentsOfFile:@"string.rtf"];
NSAttributedString* attrString = [[NSAttributedString alloc] initWithRTF:rtfData documentAttributes:nil];
此外,由于 RTF 是一种简单的文本格式,您可以将 RTF 数据转换为 NSString 并将其作为纯文本存储在字典中。
关于objective-c - 如何在 NSDictionary 中保存(和检索) NSAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22312708/