Check the array of classes for a match against a variable, and if successful, add other variables

Viewed 28

I have a function that takes as input an array of classes containing several variables.

class AlcoholVolumeStatisticValue {
var alcoholType: String
let volumeType: String
private(set) var portions: Double

var totalVolumeInLiters: Double {
    return VolumeConverter.volumeInLiters(portions: self.portions,
                                          volumeType: AlcoholVolumeType(rawValue: self.volumeType))
}

init(alcoholType: String, volumeType: String, portions: Double) {
    self.alcoholType = alcoholType
    self.volumeType = volumeType
    self.portions = portions
}

func increasePortions(by portions: Double) {
    self.portions += portions
}

}

And function

private func acceptAlcoholeVolumePerPeriod(_ values: [AlcoholVolumeStatisticValue]) {
    let chartValues = values.map({ value in
        return CircleChartValue(name: value.alcoholType,
                                volume: value.totalVolumeInLiters,
                                count: 0,
                                color: AlcoholType(rawValue: value.alcoholType)?.statisticColor ?? .black,
                                isVolume: true)
    })
    self.alcoholVolumeChartValues.accept(chartValues)
    
}

I need to check by array elements if value.alcoholType matches each other, and if successful, Add to each other value.TotalVolumeInLiters

0 Answers
Related