Answer the question
In order to leave comments, you need to log in
What's the difference in optionals in Swift?
Can't understand the difference between optionals and implicitly extracted optionals
Answer the question
In order to leave comments, you need to log in
Implicitly Unwrapped Optionals generally behave like Optionals. In fact, they are converted to Optional at every opportunity:
let x: Int! = 42
let y = x // y has type Int?
func f(_ arg: Int) { print(arg) }
let x: Int! = 42
let y: Int = x
let z = x as Int
f(x)
struct Dog {
func bark() { print("Woof!") }
}
let d: Dog! = Dog()
d.bark()
class Foo {
var x: Int!
init() {
// I can't setup x at this point :(
x = nil
}
func setup(x newValue: Int) { x = newValue }
func foo() {
// I promise that setupX() will be called before
// I will deal with x as if it was always present
print(x * 2)
}
}
let x: Int! = 42
@implicitlyUnwrapped let x: Optional<Int> = .some(42)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question