I’m a professional developer advocate building mobile apps and I admit it – I just recently began the switch over to code with Swift. My first impression? It’s got real advantages and that’s what makes Swift powerful and an obvious choice for so many developers.
I hear it all the time: ‘Swift is a modern language, for modern development.’ I now see why.
Swift is concise, yet expressive and powerful, and easy to learn.The Swift language enables developers to quickly ramp up and start building applications. It supports everything that is expected from modern language: type safety, namespacing, closures, generics, multithreading, and much more. The Swift language puts all of these great features in the hands of developers, empowering their development processes, and now, encouraging reuse across multiple properties.
Swift is easier to read, more closely resembles natural language, is more concise, and is much more approachable for developers.Swift breaks from the traditional C and Objective C syntax, and at first glance resembles something closer to ECMAScript 6, albeit with more than a few improvements. This means, less code, and it means cleaner code. Less code + cleaner code = more maintainable code, which is a big win in my book!
Swift is modern.Swift has full support for class typing, inheritance, protocols/interfaces, and all the compile-time error checking with these features – as you would expect from any modern language, but it doesn’t stop there. Swift also supports modern conventions like closures – anonymous function definitions that can be passed around like variables, computed properties – properties that are actually function invocations when referenced; also commonly referred to as “getters” and “setters”, property observers – functions that are triggered when values on an object change, and more.
Of course, let’s not forget Swift’s advantages in memory management, scoping, and optionals.
ARC, or automated reference counting, is native in Swift.ARC tracks and manages memory usage, so developers don’t have to manually deallocate variables and clean up after themselves. This means that you can write code much like you would in a managed language, where the garbage collector would clean up your memory stack when variables are no longer used. With Swift and ARC there is no garbage collector; it is a compiled language. However, the Swift compiler automatically knows when variables will no longer be used and adds the deallocation for you. This gives the developer the flexibility and ease of development, yet blazingly fast runtime performance advantage of a compiled language.
Swift is safer.This is because it introduces new language features to protect values in memory and create “safer” code. Optionals are one such feature. Optionals essentially wrap your code and inspect/protect against nullability – this prevents crashes from null pointer errors. Code such as the following will not crash if myDataInstance is nil:
let myResult = myDataInstance!.save()
The value of myResult would be nil, but your app won’t crash if myDataInstance doesn’t exist. This means fewer crashes, hopefully fewer errors, with less code on the developer’s part.
Optionals can also be chained, thus simplifying code even further. Let’s examine a few statements.
let contact = contactsManager.contacts let address = contact.address let street = address.street
Using optionals and chaining, you can simplify this down to the following, and the Swift language will handle nullability checks for you:
let street = contactsManager?.contacts?.address.street
Swift also introduced the guard statement, which can act as an if-statement on nil values, but more importantly, lets you easily and gracefully handle cases of nil or non-nil values. We could add a guard statement to the previous snippet to guarantee that any nil cases are properly handled. In addition to memory protection, Swift also supports a traditional try/catch error handling model.
Again, you put all of this together, and it means cleaner code.
Much cleaner code.
Did I also mention Swift is fast? Like, really, really fast.Since Swift is a compiled language, you get all the performance normally associated with compiled “native” code (as opposed to managed/interpreted languages). 3rd party benchmarks have even shown Swift as fast C, and in many cases much faster than other languages.
So, if you put this all together, there’s a lot to be excited about. Swift is a modern, fast, extremely powerful language that helps you write cleaner/safer/better/faster/more maintainable code, and it’s easier to pick up and learn.
Seriously, What’s not to be excited about?
If you’re new to Swift, then head over to the Swift Tour to get started and learn more about the language.