Search code examples
swiftcodable

How to make NSAttributedString codable compliant?


What is the problem?

Currently I am building an app-extension on my main app which communicates via a JSON. Theming and data is located in the JSON and is being parsed via the codable protocol from Apple. The problem I am experiencing right now is making NSAttributedString codable compliant. I know it is not build in but I know it can be converted to data and back to an .

What I have so far?

Cast a NSAttributedString to data in order to share it via a JSON.

if let attributedText = something.attributedText {
    do {
        let htmlData = try attributedText.data(from: NSRange(location: 0, length: attributedText.length), documentAttributes: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType])
        let htmlString = String(data: htmlData, encoding: .utf8) ?? "" 
    } catch {
        print(error)
    }
}

Cast a html JSON string back to NSAttributedString:

do {
    return try NSAttributedString(data: self, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
    print("error:", error)
    return  nil
}

My Question?

How to make a struct that has a property nsAttributedTitle which is of type NSAttributedString and make it codable compliant with custom encoder decoder?

Example of the struct (without thinking about codable compliance):

struct attributedTitle: Codable {
    var title: NSAttributedString

    enum CodingKeys: String, CodingKey {
        case title
    }

    public func encode(to encoder: Encoder) throws {}
    public init(from decoder: Decoder) throws {}
}

Solution

  • NSAttributedString conforms to NSCoding so you can use NSKeyedArchiver to get a Data object.

    This is a possible solution

    class AttributedString : Codable {
        
        let attributedString : NSAttributedString
        
        init(nsAttributedString : NSAttributedString) {
            self.attributedString = nsAttributedString
        }
        
        public required init(from decoder: Decoder) throws {
            let singleContainer = try decoder.singleValueContainer()
            guard let attributedString = try NSKeyedUnarchiver.unarchivedObject(ofClass: NSAttributedString.self, from: singleContainer.decode(Data.self)) else {
                throw DecodingError.dataCorruptedError(in: singleContainer, debugDescription: "Data is corrupted")
            }
            self.attributedString = attributedString
        }
        
        public func encode(to encoder: Encoder) throws {
            var singleContainer = encoder.singleValueContainer()
            try singleContainer.encode(NSKeyedArchiver.archivedData(withRootObject: attributedString, requiringSecureCoding: false))
        }
    }
    

    Update:

    In Swift 5.5 native AttributedString has been introduced which conforms to Codable.