I may have this really wrong. Noob. I've recreated what I'm doing in a playground type environment here. Basically the sender is a slider within a UITableView of other sliders. The myData is the underlying data. I want to perform a calculation to all the items of the underlying data EXCEPT the one which corresponds to the sender item. I have no idea if my closure syntax is correct. This is the first time I'm creating one.
// sender comes over as a struct
struct myStruct {
var tag: Int = 0
var value: Float = 0
}
let sender = myStruct(tag: 1, value: 199)
// some vars for the calculation
let globalTotal: Float = 597
let globalAnotherTotal: Float = 0
// an array of data structs
struct myDataStruct {
var name: String = ""
var value: Float = 0
}
var myData: [myDataStruct] = []
myData.append(myDataStruct(name: "Tom", value: 45.0))
myData.append(myDataStruct(name: "Dick", value: 16.4))
myData.append(myDataStruct(name: "Harry", value: 12.3))
// a closure to do the calculation
var calcOtherVals: (Float, Float) -> (Float) = { (startVal, senderStartVal) in
let remainingStartVals = globalTotal - senderStartVal
let remainingNewVal = globalTotal - sender.value - globalAnotherTotal
let endVal = ((startVal * (100 / remainingStartVals)) / 100) * remainingNewVal
return endVal
}
// now need to perform calcOtherVals on all the .value floats in myData EXCEPT the element at position sender.tag hopefully using filter and map
So basically I'm trying to use filter and map and the calcOtherVals closure to edit the array of structs in place. I can do this with conditionals and loops and calcOtherVals as a function no problem. Just hoping to do it more elegantly.
QUESTION: As in the code comment, I need to perform calcOtherVals on all the .value floats in myData EXCEPT the element at position sender.tag. How?
myDataand filter it to get all data based on what?myDataStructdoes not has atagproperty. could you elaborate?