Does Swift Live Up to Its Name?
- Environment Matters
- Variables
- Manipulating Strings
- Taking It Easy
Apple created the Swift programming language with the goal of making Mac and iOS software development easier for beginners as well as long-time app developers. Although very powerful, Apple's flagship development language, Objective-C, requires significant time and study to master. If you program in C or C++, you have a head start, but even then, Objective-C's syntax is unique enough to require some time for uninitiated programmers to get comfortable with it.
Swift promises to change that situation. Its very name implies that with a small investment of time and effort, you can “swiftly” learn the language and develop some semblance of an application in short order, especially compared to the same amount of effort applied to other programming languages. Surely Apple isn't just paying lip service to the idea—there must be a quantifiable way for Swift to justify its name!
In this article, we'll explore just how Swift stacks up against other languages and programming environments, and why it earns its name.
Environment Matters
In order to start a comparative analysis of Swift vs. other languages, it's helpful to consider the environment in which they are used.
Like traditional programming languages such as C and C++, Swift is a compiled language. That is, source code is digested by a compiler, which eventually emits object code for a particular processor. This traditional method of language conversion doesn't set Swift apart from C, C++, or Objective-C. What does make a difference is the fact that Swift code can also be developed and tested in a more interactive environment.
Read-Eval-Print-Loop (REPL)
With Swift's Read-Eval-Print-Loop (REPL) design, you can try out code “on the fly” and get immediate results—right from the command line. While this capability is the norm for interpreted languages like Ruby or Python, it's not a feature of compiled languages. That's one reason why Swift is a great beginner's language: You can start using it “right out of the box."
Once you've installed Xcode on your Mac, all you need to do to use the REPL is bring up the Terminal application and type the following line:
xcrun swift
For example, writing the ubiquitous “Hello world!” message to the screen in Swift is as simple as using the println() method, which takes a string as a parameter (see Figure 1):
println(“Hello world!”)
Figure 1 Interacting with the REPL in the Terminal application.
To get similar results in C, you would have to write this code in an editor:
#include <stdio.h> void main(int argc, char **argv) { printf(“Hello world!\n”); }
Once typed in, the C code still needs to be compiled and executed. Not only is it more lines of code, but it's not interactive and requires additional steps, including a compilation process. In short, it doesn't hold a candle to the ease and agility of Swift's REPL.
Playgrounds
Just when you thought Swift couldn't get any simpler, you'll discover its playgrounds. With a richer and more interactive environment, the playground is another way of testing your Swift code and learning concepts of the language. Once you've perfected your code, you can easily transfer it from the playground into a compiling app within any Xcode project by simply copying and pasting.
Unlike the REPL, which runs from the command line, the playground is part of Xcode and can be created from the File > New > Playground menu command. Type a name for the playground document, and you're ready to start using this great feature.
Figure 2 shows a playground that demonstrates variable declarations, math, and inline printing.
Figure 2 Using a playground to experiment with variable assignments.
As the figure shows, a playground gives you a linear listing of any code you write, with line numbers on the left side. The Results area on the right shows the result of each line as you finish typing it. Not only is this an easy way to reference code, but the immediate feedback is a helpful feature not offered by other compiled languages such as Objective-C or C.