iOhYes Retired

iOhYes

A podcast by iOS developers for iOS developers, delivering news, tips, and rants for professional iOS/Mac developers, with something for enterprise and indie developers alike.

Hosted by Darryl Thomas and John Sextro.



← Previous Episode   |   Next Episode →

70: Episode 70 - Demeter Defeater

May 28, 2015 at 8:30PM • 1 hour 1 minute • Wiki Entry

Automatic Reference Counting (ARC) including what's new (not much) and suggestions for when to use weak vs. unowned.

Optional Chaining including Nil protection, multiple optional calls chained and the Law of Demeter.

Generics including generic functions, generic types, associated type, type constraints and the use of the Where clause.




Show Notes & Links Presented by CacheFly

Tweet Shoutouts

The Discussion

  • Swift
    • Automatic Reference Counting
      • Same old ARC we know and (mostly) love
      • Applies only to instances of class types, as value types aren’t stored and passed by reference
      • Use weak references to avoid reference cycles whenever it’s possible for that reference to be nil at some point. If a reference will always have a value, use unowned instead.
        • All weak references must have an optional type.
        • All unowned references must have a non-optional type
      • In scenarios where one class depends on another, consider using weak implicitly unwrapped optionals instead of unowned references.
      • Delegation
      • As with Objective-c, strong reference cycles are possible when a class instance has a closure property that captures the instance
        • Swift uses “closure capture lists” to solve this
        • Example: [unowned self] in … or [weak self] in …
          • Use unowned references when the closure and captured instance will always refer to each other and will always be deallocated at the same time
          • Use weak references when the captured instance may become nil at some point.
          • Weak references are always optionals.
        • Similar to using the __weak/__strong pattern in Objective-c, but considerably cleaner
        • Did anyone manage to add something other than self to a capture list??
          • Homework: Can you add something other than self to a capture list?
    • Optional Chaining
      • Process for querying and calling properties, methods and subscripts on an optional that might be nil
      • If the optional is nil, the call returns nil
      • Law of Demeter
      • Multiple optional queries can be chained together
      • Similar to messaging nil in Objective-c, but in a way that works for any type and can be checked for success or failure
    • Generics
      • Generic Functions
      • Generic Types
        • Classes, structures and enumerations that can work with any type, similar to Array and Dictionary
      • Both use a placeholder type, indicated with a placeholder typename within angle brackets (eg: <T>). Type parameters should always have UpperCamelCase names.
      • Type constraints
        • Limits the usable types by specifying a class or protocol a type must descend from or implement, respectively
      • Associated types via typealias
        • These are basically generics for protocols, right?
      • Where clause
        • Basically type constraints for associated types
        • I was really worried they were going to throw some crazy syntax at us to "simplify" writing the where

Picks

John

Darryl

  • Let’s Build Swift.Array - Mike Ash Describes how to implement value semantics and the use of the isUniquelyReferenced API introduced in Swift 1.2

Chad

Alternative Show Titles

  • Turns out
  • Garbage Collection
  • John accepted
  • Deadly embrace
  • Retain hug of death
  • Go away
  • I’m no longer interested in you
  • I hate thinking
  • Scare away Ruby developers
  • Demeter defeater
  • ipso facto math
  • I don’t know what T is
  • The dreaded T
  • Monad land
  • T Soup
  • Advanced Age
  • You can pry my header files from my cold, dead fingers
  • So there
  • With great power comes great responsibility
  • Broom Closets
  • All arrows point to “No!”