iOhYes Retired


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 →

68: Episode 68 - Initializers Unlocked!!

May 13, 2015 at 10:00AM • 1 hour 2 minutes • Wiki Entry

Chad and Darryl continue our series on Swift. This time they take a look at Inheritance, Initializer and Deinitializers.

We cover the tweet shoutouts, homework assignments and wrap things up with our picks of the episode.

Show Notes & Links Presented by CacheFly

Tweet Shoutouts

The Discussion

  • Swift
    • Inheritance
      • Base class
      • Subclass
        • Depth of inheritance hierarchy
      • Overriding
        • Accessing super
        • Methods
        • Properties
        • Getters and Setters
        • Prop Observers
        • Preventing with `Final`
    • Initialization
      • Initializing property values vs default values
      • Automatic external names for all parameters, including the first
      • Un-named parameters using the underscore (_) when intent is clear
      • Structures automatically receive a memberwise initializer if they do not define any of their own custom initializers, even if the stored properties do not have default values.
      • If you define a custom initializer for a value type, you will no longer have access to the default or memberwise initializer for that type. If you do want both the default and a custom initializer, define the custom initializer in an extension. #extensionsyes
      • Designated initializers
        • Primary initializers for a class
        • Fully initializes all properties introduced by the class
        • Calls an appropriate superclass initializer
        • Every class must have at least one designated initializer
        • Multiple designated initializers are allowed, but it is common to have only one and rare to have more than a few
      • Convenience initializers
        • Secondary, supporting initializers for a class
        • Defined using the ‘convenience’ modifier before the init keyword
        • Can call a designated initializer with some of the designated initializer’s parameters set to default values
        • Are not required
        • Used whenever a shortcut to a common init pattern will save time or make intent clearer
      • Initializer delegation rules
        • A designated initializer must call a designated initializer from its immediate superclass
        • A convenience initializer must call another initializer from the same class
        • A convenience initializer must ultimately call a designated initializer
        • Simplified:
          • Designated initializers must always delegate up
          • Convenience initializers must always delegate across
      • Class initialization is a two-phase process
        • Phases
          • Each stored property is assigned an initial value
          • Each class is given the opportunity to customize its stored properties
        • Provides safety while allowing customization by preventing values from being accessed before they are initialized or being set to a different value unexpectedly
      • Subclasses do not inherit their superclass initializers by default…except when they do:
        • If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass’ designated initializers
        • If your subclass provides an implementation of all of its superclass’ designated initializers either through inheritance or custom implementation, it automatically inherits all of the superclass’ convenience initializers.
      • Failable Initializers
        • Defined using init? instead of init
        • Creates an optional value of the type it initializes
        • Return nil to trigger a failure.
        • Useful for
          • Invalid initialization parameter values
          • Absence of a required resource
          • Anything that prevents initialization from succeeding
      • Required Initializers
      • Stored property initialization via closures
    • Deinitialization
      • Called immediately before deallocation of a class instance
      • Defined using the deinit keyword
      • Cannot be called explicitly
      • Superclass deinit is called immediately following subclass deinit
      • Allows you to free resources that would not be freed automatically through ARC



  • The Ugly American Learns Swift - Daniel Steinberg at dotSwift 2015 The sneaky bastard hooked me by appealing to my fondness of Objective-C and distaste for Rubyists before making me realize I was the Ugly American.


Alternative show title suggestions

  • Do you like cheese? (from SurveyQuestion code example)
  • Yes, I do like cheese.
  • Functional-like Functionality
  • Semi-constant
  • Right On
  • Out of words
  • me myself and you
  • overall, nice pick