Setting up Codable Class. The array of AnyObjects is creating a compilation error:
Reference to member 'data' cannot be resolved without a contextual type
class ClassA<T>: NSObject, Codable {
// MARK: - Properties
let title: String
let data: [T] // data is an array of either Codable objects of ClassB or ClassC.
// MARK: - Keyes
private enum CodingKeys: String, CodingKey {
case title
case data
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
title = try container.decode(String.self, forKey: .title)
data = [T]()
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(
keyedBy: CodingKeys.self
)
try container.encode(title, forKey: .title)
try container.encode(data, forKey: .data) // Compilation error: Reference to member 'data' cannot be resolved without a contextual type
}
}