objective-c - 如何在 NSDictionary 中保存(和检索) NSAttributedString

标签 objective-c cocoa nsdictionary nsattributedstring

我正在尝试将 XML 数据保存并检索到文件中。

为此,我使用 NSDictionarywriteToFile:
问题是,如何使用 NSDictionarywriteToFile: 在磁盘上的文件中写入和检索属性字符串?

最佳答案

如果您需要比归档对象更可移植的东西,请考虑使用 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/

相关文章:

iphone - 帮助着色由图像蒙版/边界定义的图层并将图像覆盖在顶部

iphone - 如何对包含 N 个数字的 NSMutableArray 进行排序?

iphone - 如何将触摸事件从 UIView 传递到它下面的 UIView?

iphone - iPhone MIME 类型数据库在哪里?

objective-c - 是否有反向 "setValuesForKeysWithDictionary"- makeDictionaryWithObjectProperties?

objective-c - 返回 CATransform3D 将四边形映射到四边形

cocoa - NSTextField 吃 NSFontPanel..?

objective-c - NSCollectionView 拖放 : Most Delegate Events Not Getting Called

ios - 创建 JSON 字符串 - iOS

ios - 如何从此JSON字典中检索数据?