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

Home > Articles

Control Structures in Swift

  • Print
  • + Share This
On the surface, the control structures in Swift are the same as those in Objective-C, but once you start to dig a little deeper you will begin to find that they go beyond what was possible before in a number of ways, as Maurice Kelly explains in this excerpt from Swift Translation Guide for Objective-C Users: Develop and Design.
This chapter is from the book

Working with data is difficult if you have to do it in a completely linear manner, which makes control structures a vital part of any programming language. On the surface, the control structures in Swift are the same as those in Objective-C, but once you start to dig a little deeper you will begin to find that they go beyond what was possible before in a number of ways.

General Changes

Like Objective-C, Swift has four main control structures:

  • for and for-in loops
  • while and do-while loops
  • if conditional blocks
  • switch conditional blocks

They behave in a similar manner to their Objective-C counterparts but with some key differences you should be aware of.

Parentheses Are Optional

The parentheses that surround the conditionals of the if, for, and while structures are no longer mandatory. Similarly the parentheses that surround the switch expression are also no longer mandatory.

This for loop:

can now be written with less punctuation as:

Braces Are Mandatory

As if to provide a counterbalance, the Swift developers have made curly braces mandatory for control structure blocks that contain a single line of code. An if statement in Objective-C that takes advantage of this shortcut:

has to be fully fleshed out in Swift:

Enforcing the use of braces may be a source of annoyance to developers who like to keep single-line if statements compact, but should result in safer code for many others.

Boolean Conditions

The if conditional in C (and thus Objective-C) behaves in a manner summarized very simply: If the supplied expression evaluates to true then the if block is executed. Where things get complicated is in the number of ways an expression can evaluate to true. The following list shows the variety of positive evaluations:

  • A Boolean expression that evaluates to true
  • A Boolean expression that evaluates to YES
  • A numerical value that is nonzero (positive or negative)
  • An object pointer that is not nil
  • A pointer that is not NULL

In Objective-C, this allows us to take some convenient shortcuts. For example, testing for nonzero numbers is a common pattern:

Swift relies heavily on protocols to define behavior, and one such protocol is BooleanType. An expression can be used only as a conditional if it conforms to this protocol, and even the humble Int does not conform by default. To use a conditional, you just need to be more specific about what exactly it is you are testing:

This may seem like a pain at first, but is actually a useful way to learn some good programming practices. When we relied on the fact that a nonzero number equated to true, we also assumed that the reader of the code knew our exact meaning. This is acceptable with precise numbers, but could easily get confusing when working with enums or any other types where an assumption is being made that the reader knows the underlying values. The Swift core principle of safety is the likely motive behind these changes; making conditionals more explicit means less likelihood of using them incorrectly and getting unexpected results.

Now that we’ve covered the general differences, let’s look into the four control structures in detail.

  • + Share This
  • 🔖 Save To Your Account