Swift optionals are great

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.)

Why?

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 →