将带有 NSAttributed 字符串的字典保存到 PList 或 NSUserDefaults 中

发布于 2024-12-11 03:28:23 字数 1568 浏览 5 评论 0原文

我有一个带有 NSAttributtedString 的字典

{
    9 =     {
        9x0 =         {
            UserID = 123;
            formattedMessage = "Hi... How are you ?{\n    CTForegroundColor = \"<CGColor 0x601e460> [<CGColorSpace 0x6010020> (kCGColorSpaceDeviceGray)] ( 0 1 )\";\n    NSFont = \"CTFont <name: Cochin-BoldItalic, size: 17.000000, matrix: 0x0>\\nCTFontDescriptor <attributes: <CFBasicHash 0x6070640 [0x11f8400]>{type = mutable dict, count = 1,\\nentries =>\\n\\t1 : <CFString 0xf4d5c4 [0x11f8400]>{contents = \\\"NSFontNameAttribute\\\"} = <CFString 0x60707f0 [0x11f8400]>{contents = \\\"Cochin-BoldItalic\\\"}\\n}\\n>\";\n}";
            isDeleted = 0;
            isEdited = 0;
            lastModifiedDate = "2011-10-19 17:27:38 +0000";
            message = "Hi... How are you ?";
        };
    };
}

我尝试将此字典保存为文档文件夹中的 Plist 还有, 将字典保存在 NSUserDefaults 中。 两者都不起作用。 如果删除字典中的属性字符串,这两种方法都有效。

用于保存到文档文件夹中的 Plist 的代码:

NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *aFilePath = [NSString stringWithFormat:@"%@/ChatHistory.plist",aDocumentsDirectory];
BOOL aStatus = [aDictionary writeToFile:aFilePath atomically:YES];

用于在 NSUserDefaults 中保存字典的代码:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:aDictionary forKey:@"ChatHistory"];

有人请帮忙。我错过了什么吗?

I've a Dictionary with an NSAttributtedString

{
    9 =     {
        9x0 =         {
            UserID = 123;
            formattedMessage = "Hi... How are you ?{\n    CTForegroundColor = \"<CGColor 0x601e460> [<CGColorSpace 0x6010020> (kCGColorSpaceDeviceGray)] ( 0 1 )\";\n    NSFont = \"CTFont <name: Cochin-BoldItalic, size: 17.000000, matrix: 0x0>\\nCTFontDescriptor <attributes: <CFBasicHash 0x6070640 [0x11f8400]>{type = mutable dict, count = 1,\\nentries =>\\n\\t1 : <CFString 0xf4d5c4 [0x11f8400]>{contents = \\\"NSFontNameAttribute\\\"} = <CFString 0x60707f0 [0x11f8400]>{contents = \\\"Cochin-BoldItalic\\\"}\\n}\\n>\";\n}";
            isDeleted = 0;
            isEdited = 0;
            lastModifiedDate = "2011-10-19 17:27:38 +0000";
            message = "Hi... How are you ?";
        };
    };
}

I tried Saving this Dictionary as a Plist in the Documents folder
and also,
save the dictionary in NSUserDefaults.
Neither of it worked.
Both methods work if the attributed string in the dictionary is removed.

Code used to save to Plist in Documents folder :

NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *aFilePath = [NSString stringWithFormat:@"%@/ChatHistory.plist",aDocumentsDirectory];
BOOL aStatus = [aDictionary writeToFile:aFilePath atomically:YES];

Code used to save Dictionary in NSUserDefaults :

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:aDictionary forKey:@"ChatHistory"];

Someone please help. Am I missing something ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜未央樱花落 2024-12-18 03:28:23

一个有趣的实现。
猜猜这是一个解决方案:

https://devforums.apple.com/message/340283#340283

现在将探讨这个..

An interesting Implementation.
Guess this is a solution :

https://devforums.apple.com/message/340283#340283

will explore this now..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文