Chapter    11

Global and Local Variables

Most of the variables that you have been working with in the examples have had global scope. Global scope means that once the variable is declared, you can access its value from anywhere in your Swift program. This is in contrast to Objective-C, which couldn’t have variables with global scope.

Scope Defined

Scope refers to a portion of a program. In other words, you can access variables only from the portion of the program where the variable has been declared. Portions of the program are defined by various Swift constructs such as if statements (Chapter 23), functions (Chapter 27), and classes (Chapter 30).

Note  Scope applies broadly to other types in Swift as well as variables.

You will see curly brackets ...

Get Swift Quick Syntax Reference 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.