How to Initialize a structure with in a class?

From the class below how do I initialize chartEntry for the structure array? As it is below chartEntry is outside scope of init?

class ChartSeriesRow: ObservableObject {
    var id = UUID()
    var chartCategory: String
    struct chartSeriesData{
        var chartEntry: [ChartData]
    }
    struct ChartData: Codable, Identifiable, Hashable {
        var id = UUID()
        let chartMonth: String
        let chartSales: String
    }
    init() {
        chartCategory = ""
        chartEntry = chartSeriesData: [ChartData.chartMonth: "01",  ChartData.chartSales: "10.0"]
    }

    

}
Answered by Claude31 in 734727022

I don't understand what you write here (= then :)

        chartEntry = chartSeriesData: [ChartData.chartMonth: "01",  ChartData.chartSales: "10.0"]

In addition, you need to declare a car of type ChartSeriesData (starting with Uppercase).

This should work:

class ChartSeriesRow: ObservableObject {
    var id = UUID()
    var chartCategory: String
    struct ChartSeriesData {
        var chartEntry: [ChartData]
    }
    var chartSeries : ChartSeriesData

    struct ChartData: Codable, Identifiable, Hashable {
        var id = UUID()
        let chartMonth: String
        let chartSales: String
    }
    init() {
        chartCategory = ""
//        chartEntry = ChartSeriesData : [ChartData.chartMonth: "01",  ChartData.chartSales: "10.0"]
        chartSeries = ChartSeriesData(chartEntry: [ChartData(chartMonth: "01", chartSales: "10.0") ] )
    }

}

and yields (in playground):

  • ChartSeriesData(chartEntry: [__lldb_expr_1.ChartSeriesRow.ChartData(id: 2AA6BEB0-EBFA-404D-A0C3-48FDD043CE50, chartMonth: "01", chartSales: "10.0")])
Accepted Answer

I don't understand what you write here (= then :)

        chartEntry = chartSeriesData: [ChartData.chartMonth: "01",  ChartData.chartSales: "10.0"]

In addition, you need to declare a car of type ChartSeriesData (starting with Uppercase).

This should work:

class ChartSeriesRow: ObservableObject {
    var id = UUID()
    var chartCategory: String
    struct ChartSeriesData {
        var chartEntry: [ChartData]
    }
    var chartSeries : ChartSeriesData

    struct ChartData: Codable, Identifiable, Hashable {
        var id = UUID()
        let chartMonth: String
        let chartSales: String
    }
    init() {
        chartCategory = ""
//        chartEntry = ChartSeriesData : [ChartData.chartMonth: "01",  ChartData.chartSales: "10.0"]
        chartSeries = ChartSeriesData(chartEntry: [ChartData(chartMonth: "01", chartSales: "10.0") ] )
    }

}

and yields (in playground):

  • ChartSeriesData(chartEntry: [__lldb_expr_1.ChartSeriesRow.ChartData(id: 2AA6BEB0-EBFA-404D-A0C3-48FDD043CE50, chartMonth: "01", chartSales: "10.0")])

Thanks Claude,

follow up question.. How do I populate ChartSeriesData? I thought I could just add the following: However coding this way there is no append method.

extension ChartSeriesRow {
    func addRow(chartRow: ChartSeriesData){
         ChartSeriesRow.ChartSeriesData.chartEntry.append(contentsOf: chartRow)
    }
}

You'd better open a new thread…

You have to append to the instance, not the class. This should work:

extension ChartSeriesRow {
    func addRow(chartRow: ChartData){
        chartSeries.chartEntry.append(chartRow)
    }
}

You use as:

let aChart = ChartSeriesRow()

let newRow = ChartSeriesRow.ChartData(chartMonth: "02", chartSales: "20.0")

aChart.addRow(chartRow: newRow)
print(aChart.chartSeries)

And get (in playground):

  • ChartSeriesData(chartEntry: [__lldb_expr_1.ChartSeriesRow.ChartData(id: D69D70AF-A23C-44CA-9A7B-718B41ACC9F9, chartMonth: "01", chartSales: "10.0"), __lldb_expr_1.ChartSeriesRow.ChartData(id: 5BB0191C-DCCC-49C0-8FD5-B7BFF12B16AC, chartMonth: "02", chartSales: "20.0")])
How to Initialize a structure with in a class?
 
 
Q