I currently have this code:
if let isSaved = vm.place?.isSaved {
Text(isSaved ? "Saved" : "Want to go")
} else {
Text("Want to go")
}
How can I rewrite this to avoid having to essentially use the "Want to go" version of the Text
view twice?
If I try just:
Text(vm.place?.isSaved ? "Saved" : "Want to go")
I get the error:
Value of optional type 'Bool?' must be unwrapped to a value of type 'Bool'
In fact,
vm.place?.isSaved
returnsOptional<Bool>
which has3
cases:Thats the reason you cant use the ternary operator directly.
You can get rid of the optionality by ignoring a case like:
or by giving it a default value like:
Maybe separating the code a bit helps it to be more clear: