10 awesome Swift features for you diehard Swift naysayers

After being a die-hard Swift holdout (and fervent Objective-C defender), I dove head first into my first Swift-only project several months ago. The project was fairly new with only a few thousand lines of code, so it was the perfect opportunity to finally give Swift the honest chance it deserves.

I had many non-reconcilable issues with Swift's initial release syntax and tooling. It was simply too rough and unfinished. In recent months, however, notably with the 2.x line of releases, Swift has sincerely grown up and can finally be considered a "real" programming language. If you're still stubbornly on the Objective-C side of the fence - I get it - but consider this: Objective-C is over 30 years old; ancient by anyone's standards. And for all of Swift's kinks, it's as clean a break from the past as is possible for the Apple platform. Recent incarnations of the language drop all of Objective-C's antiquated C heritage and expresses itself through a clear, safe and reasonably terse syntax.

But why fix what ain't broke?

This is a legitimate question and there's no need to demonize Objective-C's dynamism. It has served developers well for over thirty years. So why has it been villainized of late? Clearly, there should still be a place in this world for dynamic languages like Objective-C. On the other hand, a whole class of bugs can be avoided by foregoing dynamism, while enabling compiler optimizations previously deemed impossible. Also, the dynamism/strictness debate seems to be seasonal, alternating from one to the other from time to time. I have come around and I'm now firmly pro-type safety. Inspired by functional programming, Swift also strongly encourages immutability, which in my opinion, leads to code which is easier to write and understand.

In the end, it's up to you to decide whether Swift can replace Objective-C in your app development toolbox. One thing is clear though: Apple has ceased improving Objective-C for its own sake. Instead, improvements are merely applied to serve better interoperability with Swift (like, recently, nullability and lightweight generics). All of Apple's future development efforts will be focused on Swift. It's not a bad idea to follow their lead, and now may not be such a bad time to start if you haven't already, as Apple has promised less code breakage in the post-Swift 3 timeframe.

Here are 10 Swift perks you can't take advantage of if you decide to stick with Objective-C:

  1. Default implementation with protocol extensions
  2. Easy early returns with guard let
  3. More fluent APIs with trailing closures
  4. Order-independent function parameters with default values (update)
  5. Single-line closure declaration by using order-based parameter names
  6. Easy namespacing with structs enums (update)
  7. Protocol implementation in extensions
  8. Defining multiple classes / structs / protocols / extensions in a single .swift file
  9. Auto-generated init parameters in structs
  10. Built-in support for lazy instance variables

I only have time for the first three today, so let's get to it.


1. Default implementation with protocol extensions

Protocols ("interfaces" in other languages) are "insanely great". They're one of the building blocks of software architecture and have a place in every software project. Swift's protocols are even more powerful when they are combined with extensions. They are effectively what Rubyists call mixins: bits of behavior that can be added to classes without the need for subclassing. The following example was taken from my open source toolkit for the Coordinator design pattern, called CoordinatorKit.

Consider the Coordinator and ComposableCoordinator protocols:

public protocol Coordinator: class {
    func start()
}

public protocol ComposableCoordinator: Coordinator {
    var childCoordinators: [Coordinator] { get set }
}

They simply define that any Coordinator should at least have a start() method and that any Coordinator that fits into a hierarchy of coordinators should at least expose an array of its children.

A common operation in this model would be to look up a child coordinator by its type, and return it. The following method accomplishes this recurring bit of functionality:

func findChildCoordinator<T>(type: T.Type) -> T?

To avoid having to re-implement this all over the place (or having to define it as a public function), one way to go about it would be to create an abstract superclass which implements ComposableCoordinator and implement it there. While is works, it's a bit of a code smell, because Swift has no official notion of 'abstract' classes. This enforced subclassing is also limiting to the user, because like most object-oriented programming languages, Swift only allows single inheritance. The Swift-way would be to declare this recurring functionality in a protocol extension:

extension ComposableCoordinator {
    public func findChildCoordinator<T>(type: T.Type) -> T? {
        return childCoordinators.filter({ (coordinator) -> Bool in
            return coordinator is T
        }).first as? T
    }
}

Any class which conforms to ComposableCoordinator will get the findChildCoordinator<T>() method for free, without the need for subclassing. Because this is protocol-oriented, you can mix in any amount of functionality, in a way that doesn't rely on convention and is supported by the language and tooling. Xcode will autocomplete this method signature on any compliant class, provided SourceKit isn't crashing, of course (wink wink).

2. Easy early returns with guard let

The handling of optionals is very prominent in Swift because it's one of the ways the language guarantees safety. Objective-C, in contrast, provides no safeguards around the handling of nil values, potentially leading to various types of nil-related crashes. While being so explicit about optional values increases safety, it adds programmer overhead in the form of countless optional unwrapping if let statements. Thankfully, Apple resolved the if let-nesting issue with the introduction of compound if let statements, but what is this guard let thing then?

Oftentimes, multiple optional values need to be unwrapped as a means of validation before proceeding execution of a method. To prevent many repetitions of if let (nested or not), you can employ guard let at the beginning of a scope to unwrap optionals and force an early return (and additionally throw an error or do some other kind of handling). After this point, you can simply pretend optionals don't exist.

Consider this contrived example for some hypothetical sign-in code:

func login(usernameText:String?, passwordText:String?, captchaText:String?, completion:(error:NSError?)->()) {
    // Catch empty values and call closure with an error
    guard let username = usernameText, password = passwordText, captcha = captchaText
    else {
        let error = // some error
        completion(error: error)
        return
    }

    // regular login handling continues here
}

Optional unwrapping is conveniently focused in one place, without the need for nesting or logic branching.

3. More fluent APIs with trailing closures

Since the introduction of Objective-C blocks ("closures" in Swift), Apple has been adding them to its APIs wherever it makes sense. Completion closures are by far the most common. For example:

UIView.animateWithDuration(1, animations: {
    // Animation code
})

With Swift, whenever a closure is the last parameter, you can use this more compact notation:

UIView.animationWithDuration(1) {
    // Animation code
}

Superficial, I know, but it turns closures into first-class citizens. They fit in more naturally with the rest of the language and reduce visual clutter by eliminating the closing parentheses.


Attribution

Header image courtesy of Tristan Ferne on Flickr (CC BY 2.0).

Updates

I would like to thank the following people for responding constructively to my write-up:

On reorderable parameters: Kyro38 on /r/swift correctly pointed out that this feature has actually been removed in Swift 3.. I guess it was nice while it lasted. You can find the Swift Evolution Proposal here. If you've been using this feature (like me), it won't hurt to prepare your code for this upcoming change.

On using structs for namespacing: @boolkybear suggested on Twitter that using enums would be better because they can't be instantiated, contrary to structs. True that!