【发布时间】:2021-06-01 20:05:16
【问题描述】:
我在 iOS14 上存档了一个 NSAttributedString,其中包含带有 NSTextAttachment 的图像,并注意到在 iOS13 上取消存档失败。在 iOS14 上解压成功。
记录的错误是这样的:
Error decoding string object. error=Error Domain=NSCocoaErrorDomain Code=4864
"value for key 'NS.objects' was of unexpected class 'NSTextAttachment'. Allowed classes are '{(
NSGlyphInfo,
UIColor,
NSURL,
UIFont,
NSParagraphStyle,
NSString,
NSAttributedString,
NSArray,
NSNumber,
NSDictionary
)}'." UserInfo={NSDebugDescription=value for key 'NS.objects' was of unexpected class 'NSTextAttachment'. Allowed classes are '{(
NSGlyphInfo,
UIColor,
NSURL,
UIFont,
NSParagraphStyle,
NSString,
NSAttributedString,
NSArray,
NSNumber,
NSDictionary
)}'.}
有没有办法让它发挥作用,还是我注定要在这里?
这是使用NSTextAttachment 将图像插入NSMutableAttributedString 的方式:
// Add an image:
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = image;
NSMutableAttributedString *strWithImage = [[NSAttributedString attributedStringWithAttachment:textAttachment] mutableCopy];
[s appendAttributedString:strWithImage];
这是存档行:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:str requiringSecureCoding:YES error:&error];
这是取消归档的行,它返回一个NSError 实例:
NSError *error = nil;
NSAttributedString *str = [NSKeyedUnarchiver unarchivedObjectOfClass:NSAttributedString.class fromData:data error:&error];
我在 iOS14 上创建了 NSData 实例,将其存储到文件中并在 iOS13 中读取。这是失败的时候。
【问题讨论】:
标签: ios ios13 nsattributedstring ios14 nssecurecoding