I wanted to unwrap an optional value and update it at the same time to be displayed, but I can’t figure out how to do that because the value is already unwrapped, so I find I have to update it in one If Let clause, and then read out the updated value in another:
if let tallyUpdate = dispatchTally[forProduct.name] {
let updatedTally = tallyUpdate + 1
dispatchTally.updateValue(updatedTally, forKey: forProduct.name)
}
if let tallyUpdated = dispatchTally[forProduct.name] {
print("\(forProduct.name) has been updated to (\(forProduct.name) tally: \(tallyUpdated))")
}
I would have preferred it all in one clause, but it’s a catch22.
Since you’ve already unwrapped the value, and set it to updatedTally, you can just reuse updatedTally (Logically updatedTally would have the same value as tallyUpdated When you print it out):
if let tallyUpdate = dispatchTally[forProduct.name] {
let updatedTally = tallyUpdate + 1
dispatchTally.updateValue(updatedTally, forKey: forProduct.name)
print("\(forProduct.name) has been updated to (\(forProduct.name) tally: \(updatedTally))")
}
That was exactly what I did. But the issue was that the value unwrapped was not updated inside the clause, so it only showed the previous value at the time of unwrapping.
It works fine when I tried in my playground.
It is possible that you use the wrong variable.
There are a few variables in that scope that have similar value after all.
Thanks for the conscience piece of code Nevin, I would only need two lines of code to set the values and retrieve it. However, Ben_Coheen is right, I would need to use the “Bang Operator” to get access to the value explicitly, and while only for use in the LLDB, I was taught never to use it. Although Swift will allow be to wrap the value (now an optional, thanks to String Interpolation) with a String: String(describing: productUpdated).
In the end, it turned out that Lantua was right and I had missed one of the constants, as can be seen in my original block of code, where I should have used the past tense when updating the dictionary.
Ms. Swift was a little too complicated for me, but I’m sure one day map will make more sense.