SWIFT: Nested options in a single Guard statement

I tried to protect the 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 “?” but it did not help. And “!” would be wrong here, Isn’t it?

Is it possible to solve this problem without having to use two lines or two protection statements?

Optional map function, only for this: < p>

guard let v = Float("x").map(Int.init) else {
return nil
}

I tried to protect the 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 “?” but it did not help. And “!” would be wrong here, Isn’t it?

Is it possible to solve this problem without having to use two lines or two protection statements?

Optional map function, only used for this:

guard let v = Float("x").map(Int.init) else {
return nil
}

Leave a Comment

Your email address will not be published.