如何保存(和检索)NSAttributedString在NSDictionary中 [英] How to save (and retrieve) NSAttributedString in a NSDictionary

查看:300
本文介绍了如何保存(和检索)NSAttributedString在NSDictionary中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to save and retrieve XML data to a file.

为此,我使用 NSDictionary writeToFile:

问题是,如何使用 NSDictionary 在磁盘上的文件中写入和检索属性字符串> writeToFile:

推荐答案

如果你需要比存档对象更便携的东西, NSAttributedString:

If you need something more portable than archived object consider using RTF representation of NSAttributedString:

    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并将其存储为纯文本在字典中。

Also since RTF is a simple text format you can convert RTF data to NSString and store it as plain text in a dictionary.

这篇关于如何保存(和检索)NSAttributedString在NSDictionary中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆