swift - 使 NSAttributedString 符合 Codable 会引发错误

标签 swift codable

我需要将 NSAttributedString 数据写入和读取到 json 文件中,之前使用过 answered question我可以对其进行编码,但在解码时会抛出错误。

class AttributedString : Codable {
    let attributedString : NSAttributedString

    init(attributedString : NSAttributedString) {
        self.attributedString = attributedString
    }

    public required init(from decoder: Decoder) throws {
        let singleContainer = try decoder.singleValueContainer()
        let base64String = try singleContainer.decode(String.self)
        guard let data = Data(base64Encoded: base64String) else { throw DecodingError.dataCorruptedError(in: singleContainer, debugDescription: "String is not a base64 encoded string") }
        guard let attributedString = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSAttributedString.self], from: data) as? NSAttributedString else { throw DecodingError.dataCorruptedError(in: singleContainer, debugDescription: "Data is not NSAttributedString") }
        self.attributedString = attributedString
    }

    func encode(to encoder: Encoder) throws {
        let data = try NSKeyedArchiver.archivedData(withRootObject: attributedString, requiringSecureCoding: false)
        var singleContainer = encoder.singleValueContainer()
        try singleContainer.encode(data.base64EncodedString())
    }
}

还有:

do {
    let jsonEncoder = JSONEncoder()
    let jsonData = try jsonEncoder.encode(attributedString)
    let jsonString = String(data: jsonData, encoding: .utf8)
    print("***\n\(String(describing: jsonString))\n***") // It works
    let jsonDecoder = JSONDecoder()
    let attrib = try jsonDecoder.decode(AttributedString.self, from: jsonData)
    print(attrib.attributedString.string)

}catch{
    print(error) // throws error
}

Error Domain=NSCocoaErrorDomain Code=4864 "value for key 'NS.objects' was of unexpected class 'NSShadow'. Allowed classes are '{( NSGlyphInfo, UIColor, NSDictionary, UIFont, NSURL, NSParagraphStyle, NSString, NSAttributedString, NSArray, NSNumber )}'." UserInfo={NSDebugDescription=value for key 'NS.objects' was of unexpected class 'NSShadow'. Allowed classes are '{( NSGlyphInfo, UIColor, NSDictionary, UIFont, NSURL, NSParagraphStyle, NSString, NSAttributedString, NSArray, NSNumber )}'.}

PS:我需要保留属性

最佳答案

您可以尝试unarchiveTopLevelObjectWithData取消归档您的AttributedString对象数据:

NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data)

作为结构实现的 AttributedString 应该如下所示:

struct AttributedString {
    let attributedString: NSAttributedString
    init(attributedString: NSAttributedString) { self.attributedString = attributedString }
    init(string str: String, attributes attrs: [NSAttributedString.Key: Any]? = nil) { attributedString = .init(string: str, attributes: attrs) }
}

存档/编码

extension NSAttributedString {
    func data() throws -> Data { try NSKeyedArchiver.archivedData(withRootObject: self, requiringSecureCoding: false) }
}

extension AttributedString: Encodable {
    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        try container.encode(attributedString.data())
    }
}

解档/解码

extension Data {
    func topLevelObject() throws -> Any? { try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(self) }
    func unarchive<T>() throws -> T? { try topLevelObject() as? T }
    func attributedString() throws -> NSAttributedString? { try unarchive() }
}

extension AttributedString: Decodable {
    public init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        guard let attributedString = try container.decode(Data.self).attributedString() else {
            throw DecodingError.dataCorruptedError(in: container, debugDescription: "Corrupted Data")
        }
        self.attributedString = attributedString
    }
}

关于swift - 使 NSAttributedString 符合 Codable 会引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58976895/

相关文章:

swift - 处理来自 Swift 的 FSEventStreamRef API 中的选项

swift - Swift 4 中的通用解码器

json swift4 如何设置结构?

ios - 在 Decodable 协议(protocol)中为模型数组设置容量

swift - 对成员 'close()' 的引用不明确

swift - CollectionViewCell 发送上一张图像

Swift Struct 是 Codable 但不能编码

Swift Codable Type 编码成功但解码失败

ios - 管理 Collection View 单元格中的多个进度 View Swift

ios - 单击 UITextField,我想运行一些代码