Encoding/Decoding Array of struct

I am trying to encode / decode an Array of struct...

The struct in question is itself Codable - and the compiler is happy with it.


In the following, roles is an Array of struct called Scene ...


try container.encode(roles, forKey: .roles) Compiler says ... ! Reference to member 'roles' cannot be resolved without a contextual type


roles = try container.decode([SceneRole].self, forKey: .roles) Compiler complains Ambiguous reference to member 'decode(_:forKey:)'


Any suggestions gratefully received !

I'm not sure I caught all the struct definitions, but what about something like:


try container.encode(roles, forKey: Scene.roles)


(If roles if effectively a property of Scenes)

Just a line of code does not help to solve your issue. Please show enough code. Whether your code is valid or not depends on where you write it.

Here's the code .....


struct Inverter {

let name: String

let usage: Int


enum CodingKeys: String, CodingKey {

case name

case usage

}

init(name: String, usage: Int) {

self.name = name

self.usage = usage

}

func encode(to encoder: Encoder) throws {

var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(name, forKey: .name)

try container.encode(usage, forKey: .usage)

}

init(from decoder: Decoder) throws {

let container = try decoder.container(keyedBy: CodingKeys.self)

name = try container.decode(String.self, forKey: .name)

usage = try container.decode(Int.self, forKey: .usage)

}


}


struct BaseLine: Codable {


let uniqueId: Int

let sceneText: String

let wayThrough: [Inverter]


enum CodingKeys: String, CodingKey {

case uniqueId

case sceneText

case wayThrough

}

init(uniqueId: Int, sceneText: String, wayThrough: [Inverter] ) {

self.uniqueId = uniqueId

self.sceneText = sceneText

self.wayThrough = wayThrough

}

func encode(to encoder: Encoder) throws {

var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(uniqueId, forKey: .uniqueId)

try container.encode(sceneText, forKey: .sceneText)

try container.encode(wayThrough, forKey: .wayThrough) Compiler says - ! Reference to member 'wayThrough' cannot be resolved without a contextual type

}

init(from decoder: Decoder) throws {

let container = try decoder.container(keyedBy: CodingKeys.self)

uniqueId = try container.decode(Int.self, forKey: .uniqueId)

sceneText = try container.decode(String.self, forKey: .sceneText)

wayThrough = try container.decode([Inverter].self, forKey: .wayThrough) Compiler says- Ambiguous reference to member 'decode(_:forKey:)'

}


}

Accepted Answer

You said:


The struct in question is itself Codable


but it isn't. It would need to be:


struct Inverter: Codable {


The error message really *****, though. 🙂

Seems QuinceyMorris has given you the right answer.


By the way, you have no need to define your own `init(...)`, `CodingKeys`, `encode(to:)` and `init(from:)` for both of your struct.


Swift compiler generates those things implicitly for some typical case, and your code does match the case:

struct Inverter: Codable {
    let name: String
    let usage: Int
}

struct BaseLine: Codable {
    let uniqueId: Int
    let sceneText: String
    let wayThrough: [Inverter]
}


Are you exploring how Swift works on structs and Codables? Or do you still have something hidden?

Encoding/Decoding Array of struct
 
 
Q