O'Reilly logo

Cocoa Programming for OS X: The Big Nerd Ranch Guide by Aaron Hillegass, Adam Preble, Nate Chandler

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Loops and String Interpolation

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

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

f​o​r​ ​v​a​r​ ​i​ ​=​ ​0​;​ ​i​ ​<​ ​c​o​u​n​t​i​n​g​U​p​.​c​o​u​n​t​;​ ​i​+​+​ ​{​ ​ ​ ​ ​l​e​t​ ​s​t​r​i​n​g​ ​=​ ​c​o​u​n​t​i​n​g​U​p​[​i​]​ ​ ​ ​ ​/​/​ ​U​s​e​ ​'​s​t​r​i​n​g​'​.​ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required