Publishers of technology books, eBooks, and videos for creative people

Home > Articles

  • Print
  • + Share This
From the author of


Now that we've looked at a Swift playground, let's consider the basic Swift concept of variables. A variable is the basic construct in Swift for holding values, whether that value is a number, a string, or something more complex. In the code in Figure 2, three variables are created—or, as we like to say, declared: a, b, and c.

The var keyword is Swift's way of recognizing a variable declaration. The name of that variable follows, and then a colon (:), and finally a type and an assignment:

var a : Int = 5

In plain English, this declaration says: “A variable named a of type Int (an integer, or whole number), whose initial value is the number 5.” The same sentence structure would apply to variable b declared on line 8 in Figure 2.

Line 9 declares variable c, but the assignment is the sum of variables a and b declared just prior. Notice that the type Int is left out of this declaration. Swift allows variables to be declared explicitly with the type following a colon (:), or implicitly in the manner shown on line 9 of the code in Figure 2. Implicit declaration of a variable means that Swift infers the type based on the assignment. In this case, adding the two Int variables a and b results in a new Int value; therefore, the type of Int is implied in the declaration of the variable c.

This method of variable declaration is slightly different than what you would find in C or Objective-C. For example, this snippet of C source code is equivalent to the Swift code just described:

int a = 5;
int b = 6;
int c = a + b;

A little less typing perhaps, but C has no implicit type declaration. Swift is a bit more relaxed in this case. Also, Swift statements don't have to end with semicolons, as required in C and Objective-C.

Printing Variables

One area where Swift really shines compared to other languages is the ease with which you can embed values in strings for printing or other purposes. You can see a glimpse of this feature by looking at line 11 in the Figure 2 playground:

println("Simple math: \(a) plus \(b) equals \(c)")

The beauty of this statement is that the variables a, b, and c are all embedded “inline” and directly into the string by using Swift's special syntax: a slash (/) followed by the variable name enclosed in parentheses. This string reads naturally, without fussing with formatting strings as you might see in C:

printf("Simple math: %f plus %f equals %f", a, b, c);

or Objective-C:

NSLog(@"Simple math: %f plus %f equals %f", a, b, c);

In these C and Objective-C examples, the formatting string contains codes such as %f, which act as markers to position the variables. The variables themselves are tacked onto the end of the string in the same position as in the format codes.

The fragility inherent in this style of string formatting is twofold:

  • Using incorrect formatting codes can result in unpredictable output—and in some cases, outright crashes.
  • The order of the variables is critical: Accidentally swapping the parameters b and c, for instance, will cause the statement to read incorrectly.
  • + Share This
  • 🔖 Save To Your Account