Return to Video

01-20 Ok but what about Exclamation Points

  • 0:00 - 0:02
    We saw that it's possible to declare and
  • 0:02 - 0:04
    unwrap the optionals
    with the question mark.
  • 0:04 - 0:10
    Now we'll see that it's possible to do
    both the exclamation point as well.
  • 0:10 - 0:12
    Let's look at unwrapping first.
  • 0:12 - 0:15
    If we have a variable named x,
    we can unwrap it like this.
  • 0:15 - 0:19
    The exclamation point follows the
    variable just like the question mark.
  • 0:19 - 0:22
    But if the value inside is nil,
    it explodes.
  • 0:22 - 0:25
    All right, nothing explodes,
    but it does throw an exception.
  • 0:25 - 0:28
    There's no safe way to unwrap an if
    statement with an exclamation point.
  • 0:28 - 0:31
    You can think of this as failing fast.
  • 0:31 - 0:34
    When you choose to unwrap with
    an exclamation point, you're saying that
  • 0:34 - 0:38
    you want the program to crash if
    the variable contains a nil value.
  • 0:38 - 0:41
    It communicates to your code readers
    that you are fully confident that there
  • 0:41 - 0:44
    will not be a nil value in the optional.
  • 0:44 - 0:47
    We can use the exclamation point
    to declare an optional too.
  • 0:47 - 0:50
    We follow the data type
    with an exclamation point,
  • 0:50 - 0:51
    right where the question
    mark went before.
  • 0:52 - 0:54
    But this will be a special
    kind of optional,
  • 0:54 - 0:57
    an implicitly unwrapped optional.
  • 0:57 - 1:00
    That means that any time the optional
    is used in an expression,
  • 1:00 - 1:01
    it will be automatically unwrapped.
  • 1:03 - 1:06
    It is an optional, so
    it can store a value or nil.
  • 1:06 - 1:10
    You might think of it as a very fragile
    package that automatically falls open or
  • 1:10 - 1:12
    unwraps whenever it's picked up.
  • 1:12 - 1:14
    Here too there's an explosive quality.
  • 1:14 - 1:16
    If we don't unwrap it carefully,
  • 1:16 - 1:20
    it's automatically unwrapped any
    time it's used in an expression.
  • 1:20 - 1:24
    If automatically unwraps and it contains
    a nil, then an exception will be thrown.
  • 1:25 - 1:27
    How can we use these
    dangerous types of optionals?
  • 1:27 - 1:29
    Let's look at some quick examples.
  • 1:29 - 1:33
    Essentially you only use the exclamation
    point when you're sure that the optional
  • 1:33 - 1:34
    won't have a nil.
  • 1:34 - 1:37
    An invocation of toInt is always
    going to return an optional.
  • 1:37 - 1:41
    But in this case there's not a lot of
    doubt about what that optional holds.
  • 1:41 - 1:46
    If an optional is like a package, this
    package is going to have 123 inside it.
  • 1:46 - 1:48
    We can unwrap it boldly.
  • 1:48 - 1:51
    We could of unwrapped the optional
    right as it returned.
  • 1:51 - 1:54
    In that case, x would have been created
    as an int instead of an int optional,
  • 1:54 - 1:57
    and it won't need to be unwrapped.
  • 1:57 - 2:00
    This is a nice example of how swift
    infers the type of variables when we
  • 2:00 - 2:01
    initialize them.
  • 2:01 - 2:05
    By unwrapping this optional return
    type right when it is returned,
  • 2:05 - 2:08
    we make it not an int optional but
    a plain old int.
  • 2:08 - 2:11
    And so x is declared as a plain old int.
  • 2:11 - 2:14
    How about declaring an optional
    with an exclamation point?
  • 2:14 - 2:18
    I referred to that earlier as
    the implicitly unwrapped optional.
  • 2:18 - 2:20
    When we use view controllers
    with storyboard,
  • 2:20 - 2:24
    we need properties that can be nil
    when the object is first created.
  • 2:24 - 2:27
    But we can also be confident that
    the values will get set by storyboard.
  • 2:27 - 2:30
    You will remember that this button needs
    to be an optional, but we can declare it
  • 2:30 - 2:34
    as an implicitly unwrapped optional
    with an exclamation point.
  • 2:34 - 2:37
    From now on this button property
    can be used without unwrapping it.
  • 2:38 - 2:41
    Anytime it is used in an expression
    it will be automatically unwrapped.
  • 2:41 - 2:43
    It has a slightly reckless feel.
  • 2:43 - 2:47
    If the button is storing nil when it is
    unwrapped, it will cause an exception.
  • 2:47 - 2:50
    Here we recklessly use the button and
    pay the price.
  • 2:50 - 2:54
    Because the button contained nil,
    we get an EXEC_BAD_INSTRUCTION.
  • 2:54 - 2:56
    The program really crashes.
  • 2:56 - 2:58
    It can be a little bit hard to tell
    why it crashed, since the error is
  • 2:58 - 3:03
    up here on the very first line,
    but this came from the unwrapping.
  • 3:03 - 3:06
    In reality, we'd only use an implicitly
    unwrapped optional when we have reason
  • 3:06 - 3:08
    to know that the button will be there.
  • 3:08 - 3:12
    Now that we've added the button back in,
    the compiler's happy.
  • 3:12 - 3:16
    Well, we hope that this jump starts
    a new confidence for you with optionals.
  • 3:16 - 3:18
    They're a bread and butter technique for
    swift programmers, and
  • 3:18 - 3:20
    it pays to really master them.
  • 3:20 - 3:22
    Take some time to work through
    the recommended reading and
  • 3:22 - 3:26
    a few exercises before you go
    back to IB action and IB outlets
Title:
01-20 Ok but what about Exclamation Points
Description:

more » « less
Video Language:
English
Team:
Udacity
Project:
UD788 - UIKit Fundamentals
Duration:
03:27

English subtitles

Revisions Compare revisions