Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Optionals, Protocols and Extensions - Swift by Example - SoCal Code Camp '16

kickinbahk
November 26, 2016

Optionals, Protocols and Extensions - Swift by Example - SoCal Code Camp '16

Whether you are working with Swift as your first language or if you are coming from another language, Swift has some quirks setting it apart from other languages.

We will explore some of the reasonings, implementations and usages of these elements in the language through practical examples. Some of the Swift concepts we will be looking at are Optionals, Protocols and Extensions.

kickinbahk

November 26, 2016
Tweet

More Decks by kickinbahk

Other Decks in Programming

Transcript

  1. Optionals, Protocols
    and Extensions
    Swift by Example
    @kickinbahk
    Josiah Mory

    View full-size slide

  2. Swift
    from a High Level

    View full-size slide

  3. Aims to be Objective-C
    without the C

    View full-size slide

  4. Swift is Type Safe

    View full-size slide

  5. Provide compile-time check that would
    prevent some common programming
    errors happened at run-time.

    View full-size slide

  6. Optionals require a
    discussion about NIL

    View full-size slide

  7. Reinforce the nil-check and
    offer compile-time cues

    View full-size slide

  8. Assumes a value must be
    present, unless told otherwise

    View full-size slide

  9. An optional must be
    unwrapped using a ? or !

    View full-size slide

  10. A blueprint of methods, properties,
    and other requirements that suit a
    particular task or piece of functionality

    View full-size slide

  11. A guarantee of some terms that will
    be satisfied. To the other party, the
    contract is a set of obligations.

    View full-size slide

  12. Abstraction Layer

    View full-size slide

  13. Allows us to Create
    Delegates

    View full-size slide

  14. Class that adopts the protocol
    is referred to as the ‘Delegate’

    View full-size slide

  15. One class (a delegator class) would give
    control or responsibility for some behavioral
    logic to another class called a delegate.

    View full-size slide

  16. 1. Create a delegate protocol that
    defines the responsibilities of the
    delegate.

    View full-size slide

  17. 2. Create a delegate property in
    the delegating class to keep track
    of the delegate.

    View full-size slide

  18. 3. Adopt and implement the
    delegate methods in the delegate
    class.

    View full-size slide

  19. 4. Call the delegate from the
    delegating object.

    View full-size slide

  20. Added functionality to a
    Class, Struct or Enum

    View full-size slide

  21. Adding functionality…
    Not overwriting it

    View full-size slide

  22. NO Monkey Patching From Extensions

    View full-size slide

  23. Increases Readability

    View full-size slide