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 pa
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.unarchiveTopLevelObjectWithData(singleContainer.decode(Data.self)) as? NSAttributedString 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))
}
}