Since it’s first adoption in 1988 on the NeXT system, Objective-C was the goto language for development on Apple hardware, and it still remains a popular choice to this day. A recent study on popular programming languages by RedMonk has shown that its days may be numbered. Swift, Apple’s latest language, is now tying with Objective-C for tenth place only 3 years after it’s release.
Swift was born out a desire to create a language that would be easy to learn for beginners without losing any of the functionality, performance, and security required by veteran developers. Borrowing features and design from a variety of different languages, Swift is able to offer a small learning curve while being 2.6x faster than Objective-C and 8x faster than python.
Swift vs. Objective-C
Objective-C was first developed over 30 years ago; in that time new technologies have risen and enhancements discovered. While it’s by no means a bad language, it does show its age. In addition to being beginner friendly, Swift fixes some of the annoyances of Objective-C and incorporates features that have become standard in modern languages.
- REPL (Read-Eval-Print-Loop): This may be one of the most significant differences, and one that offers the biggest advantage to beginners. REPL is a system wherein your code can be ran and the output shown in real-time. A system usually reserved for interpreted languages e.g. python, the addition of Swift Playgrounds to Xcode means you can experiment in real-time.
- Headers: Objective-C requires the use of header files for the declaration of functions and methods. While that can have it’s advantages—offering an easy overview of the functions contained within a code-block—it also can get unwieldy when dealing with large code-bases, with each class requiring two separate files. Swift completely removes the concept of header files, containing all the declarations in a single .swift file.
- Variable Typing: Strongly typed languages require that a variables type is defined within the code and don’t allow type changes. Both Swift and Objective-C are strongly typed, however Swift adds the ability of the compiler to infer the type from the initial variable assignment at compilation. While Swift doesn’t require the variable to by typed it will accept a type parameter and it’s generally a good idea to include one for future readability.
- Scope: While both languages are open-source (Swift since version 2.2), Objective-C has historical been isolated to usage within Apple’s ecosystem. Although Swift was designed with Apple’s hardware as the primary target, it also supports Linux development; a useful feature for developing the backend web applications of macOS and iOS apps for example.
- Unicode: Swift made some headlines on release due to it’s ability to support of emoji characters in variable and constant names. While entertaining, this is actually a result of its native support of Unicode characters, which amongst other things provides better international language support. Remember though, just because you can use the poop emoji for a variable name doesn’t mean you ever should.
Swift is available on both macOS, Ubunutu for full development, and iOS for practice and experimentation. On a macOS system, simply installing Xcode will get you up and running quickly. If you’re looking to try out Swift on a linux installation then head over to Swift.org to download the package for you Ubuntu version.
Once setup you can download the Swift Playgrounds app from the App Store which provides a series of interactive lessons to teach you the basics. Apple has also published two iBooks to get you started; The Swift Programming Language (Swift 4.0.3) and Using Swift with Cocoa and Objective-C (Swift 4.0.3).
If you’re new to macOS or iOS development you may want to jump straight in to writing your first app in completely Swift. If you’re coming from a background in Objective-C development however, Xcode allow you to combine both Objective-C and Swift code within the same project; this can also be a great feature to utilise older libraries that may not have been ported to Swift yet.