I am trying to guard a conversion from string to Float to Int:
guard let v = Int (Float("x")) else {
return -1
}
The swift 3 compiler complains:
value of optional type 'Float?' not unwrapped; did you mean to use '!' or '?'?
Adding "?" does not help, though. And "!" would be wrong here, wouldn't it?
Is it possible to solve this, without having to use two lines or two guard statements?
You can do it with one
guard
statement with an intermediate variable:Note: The
case
is there as a workaround for the fact thatInt(f)
does not return an optional value. (Thanks for the idea, @Hamish)Optional has a
map
function made just for this: