Swift - Map two elements from an array to a new array

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")
        }
    }

Answered by ChrisMH in 751691022

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)!)
            }
        }
    }
}

hi,

the expression allClosingValues.map { [$0.timeStamp! , $0.vooClose] } results in something that would have data looking something like

[ [a date, a double], [a date, a double], [a date, a double] , ... ]

i.e., where each element of the array is a two-element array of a date and a double, which has type [Any], and so the expression's result has type [[Any]].

however, you have defined closingValues as a dictionary of type [Date:Double], with keys being dates and values being doubles. that explains the error message.

if you're happy with a result of 2-tuples, where the result is more like

[ (a date, a double), (a date, a double), (a date, a double), ... ]

then you could use

closingValues = allClosingValues.map { ($0.timeStamp! , $0.vooClose) }

where you define

 var closingValues: [ (Date, Double) ]

to print out such a result, this should work:

for closingValue in closingValues {
  print(closingValue.0, closingValue.1)
}

if you really do want closingValues to be a dictionary, let us know.

hope that helps,

DMG

Accepted Answer

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)!)
            }
        }
    }
}
Swift - Map two elements from an array to a new array
 
 
Q