I have the following Swift code which uses enumerated().map
to index my modeStrings
:
let modeNames = [
"Rest: ",
"Misbalanced: ",
"Left Charged: ",
"High Voltage: ",
"High Temperature: ",
"Low Temperature: ",
"Low Voltage: ",
"Hibernation: ",
"Over Discharge Current: ",
"Ship: "
]
var modeStrings = [
deviceData.restMode,
deviceData.misbalancedMode,
deviceData.leftChargedMode,
deviceData.highVoltageMode,
deviceData.highTempMode,
deviceData.lowTempMode,
deviceData.lowVoltageMode,
deviceData.hibernationMode,
deviceData.overDischargeMode,
deviceData.shipMode
]
let indexAndNum = modeStrings.enumerated().map { (index: Int, element: inout String?) in
let value: String
if Int(data[28 + index]) == 1 {
value = "On"
} else {
value = "Off"
}
let formattedString = (modeNames[index] + value)
element = formattedString
}
}
My problem is I get an error Contextual closure type '(EnumeratedSequence<[String?]>.Element) throws -> ()' (aka '((offset: Int, element: Optional<String>)) throws -> ()') expects 1 argument, but 2 were used in closure body
This error goes away if I remove the inout
keyword but then I get this error: Cannot assign to value: 'element' is a 'let' constant
.
element
is referring to my modeStrings
which is of the type [String?]
. How can I assign a value to element
without using inout
? Or am I just using inout
incorrectly?