Using Swift optionals to simplify asynchronous work

There is a subtle change in the latest template update. The variables viewController, interactor, and presenter are now declared as real optionals using ?. (They were forced unwrapped optionals with ! before.)


There was a situation where it could cause a crash if presenter is deallocated and a method is invoked on it.

This happens when the interactor does some asynchronous work. Before it can finish, the user has navigated away (such as tapping the back button) from the current scene.

Continue reading →

Should you leave the data source and delegate methods in your view controllers?

This is a very common question. If you follow the MVC pattern, like all the tutorials and Apple sample code, your view controllers conform to a lot of protocols.

UITableViewDataSource, UITableViewDelegate, and UICollectionViewDataSource, UICollectionViewDelegate are the most common ones.

If your UI collects user inputs, you also have UIPickerViewDataSource, UIPickerViewDelegate, and UITextFieldDelegate.

What if you show a map? You can count on having MKMapViewDelegate and CLLocationManagerDelegate.

Almost all apps fetch data from APIs, so you also have some of these: URLSessionDelegate, URLSessionTaskDelegate, URLSessionDataDelegate, URLSessionDownloadDelegate.

I can keep going, but let’s just stop here.

Your view controllers conform to many of these Apple provided protocols, plus any custom protocols you define for your app. Each of these protocols has several delegate methods your view controllers need to implement. It can easily get out of hand.

This is one reason contributing to your massive view controllers. Let’s see how Clean Swift tackles this problem.

Continue reading →

Duplicate protocols are going away

Some of you have raised the annoyance of having to declare the same methods in duplicate input and output protocols, in separate files.

I hear ya. You’ll love that they’re now gone in the latest update to the Clean Swift Xcode templates.

Read along to find out why those good, old reasons have become compromises that need to be re-evaluated and ultimately removed.

Continue reading →

Zero configuration

The configurator in Clean Swift has two functions:

  1. Connect the VIP components using protocol conformance
  2. Set up the VIP cycle by connecting the output variables

Now that the protocols are no longer duplicated, the first function is not needed anymore. That leaves only the second.

It’s worthwhile to rethink if there’s a better way to set up the VIP cycle.

Among the feedback that I receive are:

  • Why does the configurator need to be a singleton? There is a newer, simpler way to make a singleton class.
  • Storyboards in Xcode have trouble recognizing the view controller subclass due to the protocol conformance specified by the extensions.
  • Some developers couldn’t use the templates out of the box because the configurator’s configure(viewController:) method is only invoked from UIViewController‘s awakeFromNib() method.

Keep reading to find out how each of these has been taken care of in the latest update.

Continue reading →

Clean Swift templates now updated for Xcode 8.3.3 & 9.0 and Swift 3 & 4

After many months of development and testing, experimenting with multiple projects, and initial beta testings by some of you, I have just released the Clean Swift templates to work for the latest Xcode 8.3.3 and 9.0. You can use it for Swift 3 and 4.

This update also has the following, long-awaited features:

  • Improved routing whether you use segues or not
  • Improved data passing using the all new data store protocol
  • Separated the routing process into two phases: navigation and data passing, with a clean interface
  • Removed the need for configurator in favor of cleaner setup
  • Combined input and output protocols to remove duplication
  • Renamed protocols with better names
  • Swiftier models with nested enums and structs
  • Use optionals to prevent crash in the VIP cycle when the scene is no longer in memory
  • Works whether you use storyboards to build your UI or not
  • View controller class names are now recognized when specifying class names in storyboards

I’ll be writing more about the new improvements in the coming weeks. In the meantime, you can check out the updated CleanStore sample app on GitHub to see how to use these latest templates.

A big thanks to those who tested early and provided me a long list of feedback and suggestions. I’ll read everything and see what I can push to the next update.

You can buy The Clean Swift Handbook now for a 50% early bird discount to get started with the latest templates, Xcode, and Swift.

Download the latest Clean Swift templates by subscribing below.

Get the Clean Swift Xcode Templates

Subscribe below to get my Xcode templates and learn how to apply the VIP cycle to your projects, extract business and presentation logic into interactor and presenter, navigate to different scenes using multiple storyboards, and write fast, maintainable tests with confidence to make changes.

I promise I'll never send you spam. You can unsubscribe at any time.