For many years, Objective-C was the language for Apple development, both on the Mac and in iOS. Now Apple is pushing development in Swift. Is it worth leaving all your years of experience to take on a new programming language for mobile app development? Definitely! Here are eight good reasons to move.
- Swift isn’t tied to C. C was a great language for its time, and it inspired many others, but it was designed for a different kind of machine and environment. It’s been described as a high-level assembly language, with hazardous features such as raw pointers and lack of bounds checking. Swift takes a fresh start and avoids these problems.
- Swift has cleaner syntax. The code is more readable than Objective-C. It’s harder to make accidental syntax errors. Where a block of statements is expected (e.g., after an “if”), it has to be in braces. Cases in switch blocks no longer fall through, and multiple cases can use the same statements. Function calls take arguments labelled with the parameter name, so you don’t get errors by putting arguments in the wrong order. New iteration types, such as for-in, simplify code structures.
- Swift doesn’t use header files. With Objective-C, you need to export declarations through a header file. This means you have to maintain the same information in two places, and discrepancies will result in compilation errors. With Swift, you can change just one file and recompile.
- Memory management isn’t as perilous. Swift has a couple of advantages here. First, it doesn’t allow raw pointers, except in unusual circumstances. In fact, you rarely use pointers at all in Swift. Raw pointers, which can hold arbitrary values, are the source of lots of bugs that malicious code and data files can exploit. Second, Swift has better support for Automatic Reference Counting (ARC). Objective-C offers a confusing mix of automatic and manual memory allocation. This means fewer orphaned objects and memory leaks in Swift.
- Swift supports namespaces. Each module automatically has its own namespace. Objective-C doesn’t have namespaces, so names need prefixes to avoid collisions. Swift allows shorter and more readable names.
- String handling is cleaner in Swift. This wouldn’t be hard; Objective-C’s string handling is one of its ugliest features. To do anything with strings, you have to call functions in the NSString class. Swift can concatenate and compare strings using familiar operators. Strings are Unicode-based and can be accessed as UTF-8, UTF-16, or UTF-32.
- Swift supports closures. Closures, the treatment of functions as assignable values, makes many things simpler. They’re especially valuable in asynchronous programming, where a callback function is supplied for execution after something else has completed. Objective-C implements callbacks through the delegate mechanism, which accomplishes the same thing with objects but requires more work.
- Swift is Apple’s future. There’s little question that Apple will gradually drop support for Objective-C and finally deprecate it. When OS X first came out, developers could write applications using the Carbon API, using C or C++. As Apple moved all its resources to Cocoa, support for Carbon diminished, and it was deprecated with OS X Mountain Lion in 2012.
Swift, like any language, has its quirks. The choices for when values are copied and when they’re passed by reference can seem confusing. The language has changed in serious ways with each release, forcing developers to rewrite working code. Some keywords are strange; “var” makes sense for declaring variables, but why does “let” mean a constant?
Still, Swift is a clear improvement on Objective-C. Hopefully it’s now stable. Sooner or later you’ll have to move to it, and the sooner you move, the more experience you’ll have with it, and the less legacy code you’ll have to update later.
Don’t forget to check out our iOS app templates.