English subtitles

← 01-20 Ok but what about Exclamation Points

01-20 Ok but what about Exclamation Points

Get Embed Code
1 Language

Showing Revision 2 created 04/08/2015 by Udacity Robot.

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