I have an array of core data entities, named TradingDayPrices, with 1 date and 4 double attributes. I would like to be able to copy the date value and 1 specific double into a new array. I have not been able to determine the correct syntax. With the code below I get the error "cannot assign value of type [[Any]] to type [Date:Double]"
var allClosingValues: [TradingDayPrices]
var closingValues: [Date:Double]
var selection: DataDisplayed
init(selection: DataDisplayed, allClosingValues: [TradingDayPrices]) {
self.selection = selection
self.allClosingValues = allClosingValues
switch selection {
case .VOODT:
closingValues = allClosingValues.map {
[$0.timeStamp! , $0.vooClose]
}
default:
let _ = print("Error in Data Table Structure")
}
}
I did not properly set up my closingValues variable to be an array, which is what I wanted. In addition, I went with a for loop to populate the closingValues array instead of the map functionality. The code below works. Thank you for your assistance DMG.
struct FundValues: Identifiable {
var timeStamp: Date
var close: Double
var id = UUID()
}
struct DataTable: View {
var allClosingValues: [TradingDayPrices]
var closingValues: Array<FundValues> = Array()
var selection: DataDisplayed
init(selection: DataDisplayed, allClosingValues: [TradingDayPrices]) {
self.selection = selection
self.allClosingValues = allClosingValues
switch selection {
case .VFIAXDT:
for value in allClosingValues {
closingValues.append(FundValues(timeStamp: value.timeStamp!, close: value.vfiaxClose))
}
case .PrinDT:
for value in allClosingValues {
closingValues.append(FundValues(timeStamp: value.timeStamp!, close: value.prinClose))
}
case .VOODT:
for value in allClosingValues {
closingValues.append(FundValues(timeStamp: value.timeStamp!, close: value.vooClose))
}
default:
closingValues = Array()
}
}
var body: some View {
Table(closingValues) {
TableColumn("Date") { value in
Text(dateToStringFormatter.string(from: value.timeStamp))
}
TableColumn("Closing Value") { value in
Text(currencyFormatter.string(from: value.close as NSNumber)!)
}
}
}
}