Building Blocks of Swift

Swift allows you to use variables and constants by associating a name with a value of some type. For example, if you want to store the string "Hi" in a variable named greeting, you can use a variable or a constant. You create a variable by using the var keyword. This establishes an associated value that can be changed during the execution of the program. If you do not want that value to be changed, you can use a constant. For example, you might record the number of login retries a user is allowed to have before being refused access to the site. In such a case, you would want to use a constant, as shown in this example:

let numberOfRetries = 5var currentRetries = 0

Notice that you don’t use a semicolon as you would in ...

Get Learning Swift™ Programming 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.