I'm trying to implement Codable for a class which contains a NSAttributedString, but I get errors at compile time:
try container.encode(str, forKey: .str)
error ambiguous reference to member 'encode(_:forKey:)'
and
str = try container.decode(NSMutableAttributedString.self, forKey: .str)
error: No 'decode' candidates produce the expected contextual type 'NSAttributedString'
I can get around it by using the NSData from the string, but thought this should work I would have thought
class Text : Codable {
var str : NSAttributedString
enum CodingKeys: String, CodingKey {
case str
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(str, forKey: .str). <-- error ambiguous reference to member 'encode(_:forKey:)'
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
str = try container.decode(NSMutableAttributedString.self, forKey: .str) <-- error: No 'decode' candidates produce the expected contextual type 'NSAttributedString'
}
}
NS(Mutable)AttributedString
does not conform toCodable
– vadian