I was incredibly excited, and a little surprised, when Apple announced Swift. That’s not to say that I’m not a fan of Objective-C; quite the opposite. However, Swift combined plenty of my favourite features of other languages: protocols, closures, generics, and tuples.
Over the past year and a half I’ve been dabbling with Swift and I wrote a Pusher library that lets you interact with our websocket API. The process of making the library has been really enjoyable and that’s almost entirely down to the fact that Swift has been great to work with.
If you’ve yet to delve into its charms, here are a few observations from the time I’ve spent exploring it at Pusher.
1. It’s not just for iOS
Sure, Swift works great on Apple platforms. But you can now use also use it on Linux, with Apple providing pre-built binaries for Ubuntu. This can be a real boon for developers who want to write code server-side and client-side as it opens up opportunities for sharing functionality.
Some members of the community have taken things even further and have managed to get Swift code working on Android.
2. Your chance to shape a young language
It’s not often you get a chance to shape a new language like Swift from the beginning. Everyday Swift becomes faster, more stable, and more fully-featured, all thanks to the community.
When Apple open sourced Swift they made a separate repository that will be used to house proposals for enhancements and changes to the language, called swift-evolution. Swift-evolution is designed to be the place to go for more substantial language changes.
Swift 3.0 is already being discussed in terms of what features will, and will not, be making it into the release. There is a list of the aims along with a list of some of the things that won’t (yet!) make it into the 3.0 release, such as C++ interoperability and language support for concurrency.
3. It’s an easy-ish language to learn
Apple has gone to considerable lengths to ensure that learning Swift is as easy as possible. Take the “Tour”, create playgrounds and even use the IBM Swift Sandbox if you want to play around with Swift in an online REPL.
If you’ve programmed before it’s likely that large swathes of the syntax and the concepts in the language will already seem familiar. You should feel comfortable using it in short order.
Importantly, whether you’re a complete beginner or a seasoned developer there’s a wealth of information available. Stanford has a free course on iTunes U, Ray Wenderlich has an enormous amount of content focussed on Swift and if those two aren’t enough, Coursera, Treehouse, and Udemy all offer Swift courses.
Moreover, as I previously mentioned, Swift is influenced by lots of other languages and so if you’re coming to Swift having programmed before then it’s likely that large swathes of the syntax and the concepts in the language will already seem familiar. At least from my own experience, writing Swift has never really felt difficult. In fact I’ve experienced the exact opposite in that I find writing Swift to feel completely natural, even though I’ve only been dipping in and out of it periodically.
4. Swift is built with the future in mind
Swift takes a lot of what is great about Objective-C and brings a whole host of new features that make it a language that you’ll want to use to solve virtually any problem. Swift is a fast, flexible and powerful language.
Swift takes a lot of what is great about Objective-C and brings a whole host of new language features that make it a language that you’ll want to use to solve virtually any problem. As with Objective-C after the introduction of ARC (Automatic Reference Counting) you don’t have to worry about memory management. Apple decided to keep the ARC model in Swift which means all of the necessary retain and release cycles are handled for you and there’s no need for a garbage collector.
A few highlights include:
- Objective-C’s ARC (Automatic Reference Counting) model is included, which means all of the necessary retain and release cycles are handled for you and there’s no need for a garbage collector.
- Named parameters make working with and creating APIs much easier.
- Error handling is simple, and if used in conjunction with the
deferstatements, leads to a programming style that favours early return.
- Swift supports inferred types meaning you can create strong, statically typed code quickly. There are even some functional programming patterns that are part of the language, such as
- Swift is fast. It uses the LLVM compiler to generate native code optimised for the platform that it’s running on. Just check this repository which has been tracking performance benchmarks to see what I mean.
5. Great tools and ecosystem (even if you hate Xcode)
When writing Swift you’ll more than likely be using Xcode, the IDE that Apple maintains that lets you write Swift and includes an interface builder and simulators for the various Apple-specific platforms that you can write apps for. Not a fan of Xcode? You could use AppCode or potentially even your favourite existing text editor if a member of the community has been kind enough to work in Swift auto-completion, including Sublime Text, Textmate and VIM.
Swift now has three main options for package management: Cocoapods, Carthage, and the newly-announced Swift Package Manager (SPM). The SPM was created by Max Howell, of Homebrew fame, and Matt Thompson. It was released by Apple at the same time as Swift was open sourced and looks likely to become the de facto package management solution for Swift code.
Finally, there is already a healthy continuous integration ecosystem that continued from where the Objective-C one reached. For example, Travis now usually has support for new versions of Xcode (and therefore Swift) within a matter of days of their release. This means that you can make use of some of the great testing frameworks that have been created, such as Quick, and have your Swift code be tested as soon as you push it up to GitHub.
So what are you waiting for?
As you might have guessed from the above, I’m a big fan of Swift. Apple has done a great job and the recent open sourcing of it can only make it a better language. All you need to do now is learn it!
If you want to get involved in some open source contributions to Swift projects – check out the pusher-websocket-swift library, we’d love to see what you can improve.
If you’ve already been using Swift I’d love to know your thoughts. Let me know in the comments below or @hamchapman on Twitter.