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

Visualising Protocols - Swift Mumbai #7

Visualising Protocols - Swift Mumbai #7

Speaker: Viranchee Lotia
One of the apps that I worked on as an intern had a block of code copy pasted in various view controllers. It is a good technique until when code reuse blows up, and you need the same functionality in multiple classes. One of the approaches to solve is taking that piece of repeated code out to a Protocol. Implementing a certain class to complete that functionality leaves us with only one point of failure/testing and eases maintainability of the code.

GitHub - https://github.com/viranchee
Twitter - https://twitter.com/code_magician

Presented at Swift Mumbai Chapter 7 Meetup hosted by BookMyShow
https://www.meetup.com/Swift-Mumbai/events/258465693

Eeb061c8b2816b771920da1b3e7904a3?s=128

Swift India

February 23, 2019
Tweet

Transcript

  1. VISUALISING PROTOCOLS Disclaimer: These are my thoughts, compiled

  2. OBJECT ORIENTED VISUALS Say Daddy! - KingBach

  3. PARENT - CHILD RELATIONSHIP ➤ Child has all properties of

    parent ➤ Child can override parent properties ➤ Visual: Vertical organisation Parent Child Human Coder Write Code Climb Walls
  4. PROTOCOLS == ACCESSORIES I can has superpower

  5. ACCESSORIES RELATIONSHIP ➤ Add additional functionalities to existing class, struct,

    enum ➤ Visual: Horizontal organisation ThisClass Protocol A Protocol C Protocol B Human Climb Walls Write Code
  6. //PLAYGROUND 1: PROTOCOLS

  7. DELEGATES Count the new followers baby! ;) Our Delegate

  8. DELEGATES: HANDOVER RESPONSIBILITY ➤ Simpler classes ➤ Abstract code out

    of the Classes ➤ Massive View Controllers? Design Patterns to the rescue!
  9. //PLAYGROUND 2: BASIC DELEGATION Protocol Class weak var Delegate: Protocol

    Delegate Class func download () -> () var delegate: Protocol? = DelegateClass() delegate.download()
  10. “➤ “You should also be careful about creating retain cycles.

    Most often, delegate properties should be weak. If an object must absolutely have a delegate set, consider adding the delegate as an input to the object’s initializer and marking its type as forced unwrapped using ! instead of optional via ?. This will force consumers to set the delegate before using the object.” -Joshua Greene. “Design Patterns by Tutorials”
  11. PLAYGROUND 3: THE TABLEVIEW DELEGATES

  12. CODE REUSE BY CTRL V ➤ Want to reuse functionality?

    ➤ Copy and Paste ➤ Abstract out the required functionality, this time we use delegates ➤ Optimum strategy: Works fine until pasted for the 3rd time, then delegate it
  13. //PLAYGROUND 3: ABSTRACT OUT CLASS METHODS TO DELEGATES

  14. @code_magician

  15. CREDITS ➤ @SwiftIndiaGroup ➤ @_vipuls ➤ @_YogeshSingh ➤ @rwenderlich ➤

    Swift online, blogging and publishing community