Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is an Optional meant in Swift?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 409
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: