Loops and String Interpolation

Swift has all the control flow statements that you may be familiar with from other languages: if-else, while, for, for-in, repeat-while, and switch. Even if they are familiar, however, there may be some differences from what you are accustomed to. The key difference between these statements in Swift and in C-like languages is that while enclosing parentheses are not necessary on these statements’ expressions, Swift does require braces on clauses. Additionally, the expressions for if and while-like statements must evaluate to a Bool.

Consider this very traditional C-style loop, written in Swift:

for var i = 0; i < countingUp.count; ++i {
    let string = countingUp[i]
    // Use 'string'
}

You could ...

Get iOS Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.