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 nsattributedstring.
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 {}
}