Notes from WWDC 2015: Cocoa Touch Best Practices

Keeping up with the latest best practices is important, but when you’re working full time, it’s hard to find the time to do it. You either have to make time during the work day when you’re already overwhelmed or make time during your nights and weekends when you’d really prefer not to be working.

I was able to make some time to watch and take notes on the Cocoa Touch Best Practices session from WWDC 2015, and there are some great tips in it. If you have time, you should watch it, but since you’re busy and probably don’t have time, you can just read the notes below. The tips about Auto Layout were of particular interest to me, so pay extra attention to those. I especially liked the one about specifying exactly the constraints you need – no more, no less.

WWDC 2015: Cocoa Touch Best Practices

Launch Quickly

Return quickly from applicationDidFinishLaunching:

  • defer long running work
  • app will be killed if too much time passes
  • use background queues – not main queue

Launch Quickly Again

  • remove resources in didEnterBackground (apps using most memory in background are the first to be killed by iOS)

Leverage Frameworks

  • reduce maintenance burden
  • get improvements for free (ex: swipe gesture to go back)
  • focus on what makes your app special

Properly manage version change

  • target two most recent major releases (lots of users, less maintenance burden)
  • include version fallbacks (version == 9.0 should be version >= 9.0) or use Swift 2.0’s new if available check

Views and View Controllers

Layout on Modern Devices

6 and 6 Plus: possible dimensions is large

  • don’t build layouts for specific dimensions
  • avoid hard-coded layout values
  • either or both dimensions can scale
  • don’t think about portrait or landscape: size classes instead
  • some size thresholds trigger major change: use size classes! (iPhone 6 plus landscape same as iPad portrait)

Properties, not tags!

avoid setTag: and viewWithTag:

  • possible collisions with other code
  • no compiler warnings
  • no runtime errors

instance variables and properties are better alternative


var imageView: UIImageView

Make Timing Deterministic

  • don’t use NSTimer for animation timings

Leverage UIViewControllerTransitionCoordinator:

  • animate alongside a transition
  • get accurate completion timing
  • support interactive and cancelable animations (nav swipe gesture – back & forth, decide to cancel)

Auto Layout

Modify Constraits Efficiently

  • manage in most efficient way
  • identify constraints that get changed, added, or remaved
  • unchanged constraints are optimized
  • avoid removing all constraints
  • keep references to them

just remove/add the constraints you need

Constraint Specificity

say exactly what you need – no more, no less

  • duplicates are implied by existing constraints
  • duplicates cause performance issues

De-dupe constraints!

Create flexible constraints

  • avoid hard-coded values
  • describe constraints using bounds

Fully specify constraints

  • underspecification generates ambiguity
  • ambiguity is undefined

Testing and debugging

-[UIView hasAmbiguousLayout]: call on UIWindow for entire view tree

-[UIView _autolayoutTrace]

Table and Collection Views

“almost every app out there”

Use self-sizing cells

introduced in iOS 8

  • fully specify constraints
  • width = input; height = output
  • try adding height constraint to content view: height = label height + margins (just for testing purposes?)

Animating Height Changes

Don’t change model and call reloadData on the entire table. Instead:

  1. tableView.beginUpdates
  2. Update model
  3. Update cell contents (cellForRowAtIndexPath, change contents)
  4. tableView.endUpdates

Fast collection view layout invalidation

Photos app: header view, cells

  1. invalidate on bounds change
  2. build targeted invalidation context (just the header view)
  3. repeat as necessary

Get up to date on Swift with the 5-Part Guide below – and spend your nights and weekends relaxing and enjoying the time off.