Giter Site home page Giter Site logo

swift-optionals-readme-dc-web-career-010719's Introduction

Optionals

Drawing

Life shrinks or expands in proportion to one’s courage. -Anais Nin

Overview

In this lesson we'll introduce a new data type, optionals.

Learning Objectives

  • Set an optional variable to a valueless state by assigning it the value nil
  • Use an if statement to find out whether an Optional contains a value by comparing the Optional against nil.
  • Use optional binding to find out whether an optional contains a value, and if so, to make that value available as a temporary constant.

Video

Note that this video covers optionals and goes into how optionals work with the Dictionary type (which you haven't learned about yet) by the end of this video. You can make an attempt to watch it all the way through or you can come back to the second half of this video after you learn about the Dictionary type in the next section.

Optional Values

Pets are great, aren't they? Isn't it nice to come home and a puppy that jumps up at you, excited to see you again after a long, hard day? Or a cat that climbs on your lap and walks all over your keyboard when you're trying to study Swift programming? Isn't it great that everyone has a furry (or scaly, or feathery) friend to keep them company?

Wait. Not everyone has a pet :(. When you first meet someone, it's pretty hard to know if they have a pet, unless you ask them. Until you do, you never know when they have an animal companion or not.

Situations like an unknown pet situation come up a lot in programming, too. You've already learned that constants and variables are associated with a value. The problem is that sometimes you don't know what that value is, or if it even exists, when you declare a constant or variable. It's like the pet situation! You might make a new friend but not immediately know if they have a pet or not.

Imagine you're writing a program to track a new acquaintance's pet situation. You want to track your friend's pet's name in a variable called petName, but initially you don't even know if they have a pet. What do you do?

Swift deals with this sort of situation through the use of a type called an Optional. An Optional is a way of saying, "I have a piece of data I want to keep track of, but I don't know if it even has a value yet." In Swift, you'd declare an Optional variable like this:

var petName: String?

Let's break down that line of code. You've seen variable declarations (using the var keyword) before, so that's nothing new. But what's with that type? It looks like a String, but it has a question mark at the end. What's up with that?

That type is actually an optional string. It means that a String value may be present, or it may not exist. Rather than saying, "petName is a String," you're saying, "petName is a String?"

Enter that line of code into a new playground. Check out the value that the playground prints for petName in the right-hand sidebar. Notice anything unusual?

petName

The playground tells you that the value of petName is nil. In Swift, nil is used to represent an unknown or non-existent value. It's a placeholder until the variable's value is known. It's like when you first meet someone, and don't know if they have a pet or not. They might have a pet, in which case if you talk to them for a while, you can learn its name; or they might not have a pet at all, in which case, you'll never get a name for their pet, because it doesn't exist (the name or the pet)!

Try Option+clicking on petName in the playground. Notice that Xcode reports that the type is of String?not String. It's an optional string!

Optional string

Let's say you talk to your new friend a bit, and you mention that you have a puppy and he's awesome. And your friend says, "I have a turtle! His name is Scooter and he's the best!"

Now you can fill in the missing information in your program:

petName = "Scooter"

Put that in your playground. You'll notice that playground shows you the current value of petName in the sidebar. It's the string "Scooter".

Scoot

So that means petName is now a String. But wait...before, you learned that variables can't just change type. How did petName go from being a String? (an optional string) to a String when you changed the value?

Try Option+ clicking on the petName in the second line of code in your program. What do you see?

petnameType

Xcode still reports the type is a String? (an optional string). The type didn't change at all!

You can see this more obviously by adding a print() statement to your playground:

print(petName)
// prints Optional("Scooter")

Yep, still an Optional.

Here's an important lesson: Optionals may have a value, or they may be nil. Regardless, they are always an optional. As you learn while writing Swift code, the Swift compiler can't know before your program is run if petName has a value or not, just like you didn't know before you talked to your friend if they had a pet or not. Even when you found out, the type of that data was still Optional.

Working With Optionals

A problem remains: When you print an Optional variable, you get that ugly "Optional(...)" bit in your output. Wouldn't it be nice to not have that part?

When working with Optionals, you have to unwrap them to get the actual value, just like you had to ask your friend the name of their pet; you couldn't just assume they had one. Swift provides several ways to unwrap an Optional to get the value inside of it.

Remember: An Optional value may be nil, though; it may not have a value at all. The reason Swift wraps unknown values in an Optional type is because it forces you to deal with the possibility of unknown or non-existent values. This helps makes your programs less prone to crashes when working with possibly unknown data.

To unwrap an Optional value, you have to first determine if a value even exists, and then get that value. Remember that an unknown value is represented by nil. Knowing what you know about both Optionals and if/else statements, how might you test an Optional value to see if it is nil or a real value?

If you said if petName != nil, give yourself a pat on the back, because you're right! You can check to see if a value in an Optional exists by testing if it is equal to nil or not. Try this if statement in your playground:

if petName != nil {
    print("My friend's pet is \(petName)")
} else {
    print("My friend doesn't have a pet :(")
}

// prints "My friend's pet is Optional("Scooter")"

Voilà! You checked to see if petName != nil, and since it isn't, the first branch of your if statement was executed. Try going back and removing or commenting out the second line of your playground (petName = "Scooter") and see what is printed instead.

But wait. We said that unwrapping an Optional wouldn't print that "Optional(...)" bit in the console, but it's still being printed! That's because you haven't actually unwrapped the value yet—you've only checked to see if it's nil or not.

To unwrap an Optional, you use a Swift feature called optional binding. In optional binding, you check to see if a value exists (is not nil) and set it equal to a constant. That constant is now available for use within the first branch of the if statement. More important, it is bound to the actual value wrapped by the Optional.

Try this in your playground:

if let petName = petName {
    print("My friend's pet is \(petName)")
}

// prints "My friend's pet is Scooter"

What do you see in the console now?

You should see "My friend's pet is Scooter". Look—no more "Optional(...)" stuff!

Let's break down that if statement a bit. Within the condition of the if statement, you wrote let petName = petName. That looks like a constant definition, doesn't it? Well, it is. What that line says is this: "If petName has a value, unwrap it and assign it to the name petName within the body of this if statement." You can then use petName to refer to the unwrapped value (but only within the if statement).

Note that you don't have to use a constant with the same name as the Optional value. This will work, too:

if let friendsPet = petName {
    print("My friend's pet is \(friendsPet)")
    print("petName is still an Optional! \(petName)")
}

// prints "My friend's pet is Scooter"
// prints "petName is still an Optional! Optional("Scooter")"

Note that when you bind the unwrapped value of petName to a different name, petName will still refer to the original Optional value. (That's why Swift programmers will often bind the unwrapped Optional value to a constant of the same name, to avoid using the wrong constant or variable name in the if statement.)

Try commenting out the second line of your playground (petName = "Scooter") and see what gets printed now.

Another thing to note: When unwrapping Optionals, you don't need to have an else branch in your if statement. If the Optional is nil and you didn't write an else branch, nothing will happen. Sometimes you want to do nothing, and sometimes you want to do something different if the Optional is nil. Either way, Swift forces you to deal with nil values.

Optionals play a big part in Swift programming. They may seem a bit confusing, but using them can lead to programs with fewer errors, and as you use them more, they'll quickly become second nature to you. Values may be optional, but knowing how to handle them isn't!

View Optionals on Learn.co and start learning to code for free.

swift-optionals-readme-dc-web-career-010719's People

Contributors

jimcampagno avatar annjohn avatar mdippery avatar pletcher avatar jawwad avatar jellybeanjohnny avatar snags88 avatar cklynch2 avatar

Watchers

Kevin McAlear avatar  avatar Victoria Thevenot avatar Belinda Black avatar Bernard Mordan avatar  avatar Joe Cardarelli avatar Sara Tibbetts avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar Jaichitra (JC) Balakrishnan avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Scott Ungchusri avatar Nicole Kroese  avatar Lore Dirick avatar Lisa Jiang avatar Vicki Aubin avatar Maxwell Benton avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.