How I learned to love Swift as an Objective-C developer

Having been coding in Objective-C for getting on for a decade now, I’d viewed Swift as something of a minor irritation – yet another new thing to have to learn. I’d dabbled in it on occasion when trying to develop new features for PrayerMate – but given very limited available time, I’d often find the overheads of having to struggle with an unknown language far outweighed any potential benefits and I reverted back to Objective-C.

With the advent of SwiftUI last summer it was clear that the tide was turning, and I made a resolve to write all new code in Swift wherever possible, and actually discovered that it wasn’t quite so bad as previous experiences had led me to believe. I’m not able to use SwiftUI yet in PrayerMate because I still support back to iOS 10, but it was increasingly obvious that Objective-C’s days were numbered. In fact, as far as I’m concerned, the real motivation for adopting Swift has not been Apple at all, but Stack Overflow – increasingly the answers there are all related to Swift, with fewer and fewer providing Objective-C alternatives.

But then over the autumn I started developing a new hobby app (still mostly under wraps) and since it was just for fun I decided to go all out and do it 100% Swift, using SwiftUI and lots of other things that I wasn’t able to explore with PrayerMate. And I have grown to not only tolerate Swift, but to LOVE it. Coding in Objective-C feels positively stone aged now by comparison.

Here are the three features I discovered that helped me to learn to love Swift:

1. Type inference

I’m not a very good one for reading the manual, but with a background in C++ I’d previously assumed that you had to explicitly state the type of each of your variables in Swift. I’d been writing code like this:


let a : Int = 5

What a pain! And totally unnecessary. Swift does all the hard work for you:


let a = 5 // : Int is inferred
let b = [[1,2,3],[4,5,6]] // [[Int]] is inferred

2. Guard statements

Previously my experience with using Swift and UIKit was a complete swamp of optionals and having to explicitly use “!” everywhere and just hating it. Discovering “guard” statements and the ability to turn an optional in to a non-optional then has been a revelation:


if ((view != nil) && (view!.frame.size.height > 5)) {
...
}

can instead become:


guard let v = view else { return }
if (v.frame.size.height > 5) {
...
}

3. Functional programming features

Combining the two features above, and it then becomes insanely easy to write the kind of functional code that I’m used to writing as a Ruby developer:


let b = myArray.map { doSomethingCleverWith($0) }
let b = myArray.compactMap { $0 > 5 ? otherFunc($0) : nil }

…only it’s better than Ruby because the compiler checks in advance that your transformations are actually doing what you expect them to by type checking the results for you.

Conclusion

There’s more that I could have said, but all in all I’m really glad to have gone all out on Swift for this new project, and it has given me an appreciation for this language that was not at all my experience previously. If you’ve now already taken the plunge, give it a try!