Just hit a problem I don’t understand…
Working with:
struct SightingList {
var listRegion: ListRegion?
private var name: String?
//number of other properties
}
struct ListRegion {
public var centre: CLLocationCoordinate2D
public var radius: Int
public var locationName: String
}
I’m trying to encode SightingList via Encodable and a JSONDecoder. I’ve extended CLLocationCoordinate2D to make it Codable, and can encode/decode both CLLocationCoordinate2D and ListRegion instances.
However when I extend SightingList to Codable (I need to do this manually as there are properties such as closures I don’t want to encode in there) as below it doesn’t work.
extension SightingList: Encodable {
enum CodingKeys: String, CodingKey {
case name
case region
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(_name, forKey: .name)
try container.encode(listRegion, forKey: .region)
}
I get the compiler error: Reference to member ‘region’ cannot be resolved without a contextual type. Substituting another property for listRegion in the encode(forKey:) fixes the problem, so it must be something to do with the nature of listRegion.
I can work round this by ‘flattening’ the listRegion struct in encode(to:) but I’d like to understand why I get the error, and ideally how to avoid it. The only time I’ve seen this sort of error before is working with underlying ObjC types/libraries that expect an AnyObject (which obviously a struct isn’t). As I understand Codable should cope with the above data structure as it can cope with structs and can encode a top level as long as it can encode the child properties.
Any ideas?
Thanks.