Revisiting Swift

A long time ago, I wrote native iOS apps with Swift.

Then I didn’t for a while.

Now I do again.

I used to hate Swift. I thought that it was inconsistent because you could explicitly or implicitly declare a variable type. There were other reasons too, but my dislike for Swift was mostly because it was a bit different than other C-based languages.

But after getting comfortable with Golang, I can now appreciate Swift as a modern language that solves common issues in nice ways:

1.) Guard Statements
In any programming language, we have to check various conditions before doing work. Usually you’d check for a null value or attempt to cast a value to another type. Depending on your coding style, it might take several statements to do that. In Swift, it can be done in a single statement. And it’s specifically designed so that you can use the value outside the block scope, encouraging flatter code rather than nested chaos.

2.) Protocols
Nothing new here. In Java and C++, they’re called Abstract Classes. A base class contains method signatures than must be implemented in the derived class. Any good IDE will offer to fill in the stubs for you (Android Studio does for Java), and Xcode does for Swift.

I just find that Swift’s protocols are simpler to work with as they don’t require any special keywords (in Java, you’d need to explicitly mark each method as abtract and in C++ you’d need to mark them as virtual). In Swift, you can make a protocol just like a class, but omitting the implementation.

3.) Tuples
It’s when you can treat multiple values as a single value. Golang has this too. Sometimes it’s nice to not need a struct for tossing around multiple values.

It’s a shame that the language is so intrinsically associated with the Apple ecosystem (iOS and OSX). There are- of course- multi-platform solutions currently out there for working with Swift on Android, Linux and Windows… but those projects tend to resemble experiments rather than production-grade solutions. Swift is nice. But not nice enough to choose over other languages if given the chance.

Leave a Reply

Be the First to Comment!

wpDiscuz