O'Reilly logo

Swift Quick Syntax Reference by Matthew Campbell

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

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 ...

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