Hi Readers!
An Optional is just an enum in Swift.
enum Optional<T> {
case None
case Some(T)
}
Here <T> is generic similar to an Array<T>
<T> could be anything like Strings, Dictionary, Array etc.
Here are some examples:
Example 1:
let a1 : String? = nil
is similar to
let a1 = Optional<String>.None
Example 2:
let s2 : String? = "hello"
is similar to
let s2 : String? = Optional<String>.Some("Hello")
Example 3:
let a : Array? = ["hello","hi"]
is similar to
let a = Optional.Some(["hello","hi"])
I hope this explains what optionals are.
0 Comment(s)