根据已接受的答案这个问题 https://stackoverflow.com/questions/2626667/saving-custom-attributes-in-nsattributedstring/我写了以下代码:
NSData* somedata;
somedata=[NSKeyedArchiver archivedDataWithRootObject:ts];
其中 ts 是一个 NSAttributedString,其中填充了一些文本和一些属性(在本例中为颜色)。
当我执行此代码时,我收到此错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0x6eb5b90'
我是 NSCoder 领域的新手,但上述问题的答案让我觉得这就是我所要做的。是吗?我错过了什么?
EDIT:
在这种情况下,无法识别的选择器被发送到 NSAttributedString 中的颜色属性。当我像这样初始化字符串时:
NSAttributedString *ts = [[NSAttributedString alloc] initWithString:text attributes:self.currentAttributeDictionary];
字典是这样构建的:
self.currentAttributeDictionary=[NSDictionary dictionaryWithObjectsAndKeys:
[self.currentColor CGColor],(NSString*)kCTForegroundColorAttributeName,
nil];
字典的 NSLog 会产生这样的结果:
New dictionary is: {
CTForegroundColor = "<CGColor 0x6eb5b90> [<CGColorSpace 0x6e968c0> (kCGColorSpaceDeviceRGB)] ( 1 1 0 1 )";}
上面 CGColor 的地址与错误消息中的地址匹配。