Arrays in struct/class are not printing in console

Basically, what I want to do is print a full json object that has a number of arrays inside it. For the sake of simplicity, I'll just paste two of the structs below:

struct Incomes : Codable {
    var *****_monthly_amount: Int = 0
    var countable_group: String = ""
    var year: String = ""

    enum CodingKeys: String, CodingKey {     
        case *****_monthly_amount
        case countable_group
        case year
    }
}

struct Person_details :  Codable {

    enum CodingKeys: CodingKey {
        case age
        case work_status, marital_status
        case pregnant, disabled, attending_school, minimum_employment_over_extended_period
    }

     var age: Int = 18
     var minimum_employment_over_extended_period: Bool = false
     var work_status: String = ""
     var pregnant: Bool = false
     var attending_school: Bool = false
     var disabled: Bool = false
     var marital_status: String = ""
}

These two are connected to a struct: Household :

struct Household :  Codable {

     var receiving_benefits : [String] = []
     var energy_crisis : Bool = false
    var utility_providers: [String] = [""]
    var person_details: [Person_details] = []
    var Income: [Incomes] = []

    enum CodingKeys: String, CodingKey {

        case receiving_benefits = "receiving_benefits"
        case energy_crisis = "energy_crisis"
        case utility_providers = "utility_providers"
        case person_details = "person_details"
        case Income = "incomes"
    }

}

Finally, a class that connects it all:

class Json4Swift_Base : ObservableObject, Codable {
    @Published var household = Household()
    enum CodingKeys: String, CodingKey {
        case household = "household"
    }

    init() { }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(household, forKey: .household)
    }

    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        household = try container.decode(Household.self, forKey: .household)
    }
}

Followed by a @StateObject & @State's:

    @StateObject var Base = Json4Swift_Base()
    @State var user = Household()
    @State var personDetails = Person_details()
    @State var Income1 = Incomes()

When a button is selected, I have this to print out the json in the console:

                let encoder = JSONEncoder()
                encoder.outputFormatting = .prettyPrinted
                do {
                    let data = try encoder.encode(Base)
                    if let str = String(data: data, encoding: .utf8) {
                        print("\(str)\n")
                    }
                } catch {
                    print("---> error: \(error)")
                }
                

It prints this with Empty arrays:

{
  "household" : {
    "assets" : [
    ],
    "person_details" : [
    ],
    "utility_providers" : [
      ""
    ],
    "incomes" : [
    ],
    "receiving_benefits" : [
    ],
    "region" : "PA",
    "residence_type" : "rent",
    "received_maximum_benefit" : {
      "cip" : false
    },
    "at_risk_of_homelessness" : false,
    "property_tax_past_due" : false
  }
}

When I print personDetails by itself and not Base, I get the person_details, but why can't I get person_details (or Income) when Base is printed (Since it's all connected) ? Why would it come up as an empty array? I have these values connected to toggles & TextFields but they're not printing. Thanks for any help!

Answered by wzebrowski in 707288022

After playing with it some, doing this seemed to work. Adding ()

     var person_details = [Person_details()]      var Income = [Incomes()]

Accepted Answer

After playing with it some, doing this seemed to work. Adding ()

     var person_details = [Person_details()]      var Income = [Incomes()]

Did you try stepping through the codes and see whether the structs got filled in when you called Json4Swift()? It seems like you might not be getting the data you need with:

required init(from decoder: Decoder) throws {         let container = try decoder.container(keyedBy: CodingKeys.self)         household = try container.decode(Household.self, forKey: .household)

You have the try function but no catch so if it fails you wouldn't have any idea. So maybe try stepping through that function and see whether your container and household are getting populated correctly.

Arrays in struct/class are not printing in console
 
 
Q