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 Slide

  2. Swift
    from a High Level

    View Slide

  3. Aims to be Objective-C
    without the C

    View Slide

  4. Swift is Type Safe

    View Slide

  5. Optionals

    View Slide

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

    View Slide

  7. Optionals require a
    discussion about NIL

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  11. Protocols

    View Slide

  12. Blueprint

    View Slide

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

    View Slide

  14. View Slide

  15. Contract

    View Slide

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

    View Slide

  17. View Slide

  18. Abstraction Layer

    View Slide

  19. Allows us to Create
    Delegates

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  26. Extensions

    View Slide

  27. Added functionality to a
    Class, Struct or Enum

    View Slide

  28. Adding functionality…
    Not overwriting it

    View Slide

  29. NO Monkey Patching From Extensions

    View Slide

  30. Increases Readability

    View Slide

  31. Thank you!

    View Slide