Exploring Different Behaviours in Swift Enums

I have a dilemma; the Swift enum is a first-class type and, if we’re to obey the laws of encapsulation, an enum type should own its functionality. But can we do this in a nice way, when its behaviour is different for each value in the enum?

In this post, we’ll explore the old Java type-safe enumeration pattern, revisit my old enums are meant for switching blogpost, and try to combine the lot.

Read more

Data-driven Screen Dispatching for iOS

The Coordinator Pattern is rather trendy at the moment. Soroush Khanlou and Paul Hudson have both written very fine blogposts about it, and I saw Paul talk about coordinators - briefly - at iOSDevUK in 2018. There are some great benefits from using Coordinators, but I can’t help feeling it doesn’t go far enough.

I propose that app navigation should be driven by data.

Read more
ios

SwiftMock - a Mocking Solution for Swift 4.2

In September 2015, I tried to write a mocking framework for Swift. It was over-engineered, difficult to use and quickly abandoned; I decided instead that simple fakes would suffice.

That all changed earlier this year. I was writing much more Swift, both at home (for the Nearly Departed rewrite) and in my day-job, and realised that mocks really are necessary if we want to thoroughly test how our classes interact with collaborators.

tl;dr: SwiftMock is available on GitHub.

Read more

Accessing Swift Arrays Without Counting on Fingers

I have a confession to make: I’ve been writing code for 35 years, but still need to count on my fingers when coding an array range-check.

It goes something like this:

  • I imagine I have an array containing four elements
  • I hold up four fingers
  • I count “zero, one, two, three”, then think “so if I ask for index four, it will break, so I need to range-check…”

OMG, 35 years. I’ve probably done this thousands of times, and the range-check rules still won’t stick in my brain.

But here’s a sneaky way to avoid that range-checking code, and I’m not ashamed to say that I use this extension for real.

Read more

Swift Extensions for Protocol Conformance Considered Harmful

I’ve come to the conclusion that using Swift extensions to make types conform to protocols is rather an anti-pattern. Let me explain why.

Read more

Handling widgetPerformUpdate in an iOS Today Extension

Apple’s documentation about the NCWidgetProviding protocol, and widgetPerformUpdate: function in particular are rather sparse, and most posts on StackOverflow seem to have a very simplistic view about how this callback should be coded. So here are my tips.

Read more

Enums Are Meant for Switching

I’m rewriting Nearly Departed, and working on a large iOS project in my day-job. While working on both of those projects, I’ve started to rely on two unofficial rules for enumerations:

  1. Enumeration values should always be checked with switch, not if
  2. default: cases in switch statements are bad
Read more

Rewriting Nearly Departed (Part 4: Storage and Sync)

This post gives an overview about how Nearly Departed Routes are stored and synced between iPhone and Apple Watch.

Read more

Rewriting Nearly Departed (Part 3: Rewrite Plans)

So here’s my plan for this rewrite of Nearly Departed.

Read more

Rewriting Nearly Departed (Part 2: Tech Background)

This post will give a little of the technical background to Nearly Departed and explain some of the reasons for the rewrite.

Read more