Learn Objective-C: Variable Scope

You can control a variable’s scope, or from where in your program it is accessible.

Language Directives

Up until now, we have seen that instance methods can access instance variables- a class can access its own variables (makes sense, and that’ll never change). More interesting is that subclasses can also access those same instance variables- that’s the default option. But you can change this:

These directives act as switches, signifying that all the variables that follow are to be of that scope until another directive or a closing brace is encountered.

@interface Printer : NSObject
{
     @private
          NSInteger jobPageCount;
          double tonerLevelPercentage;
     @protected
          NSString *brand;
}
// …
@end

The @public directive allows the variable to be accessed by other methods outside of that class. Making instance variables public is considered a poor programming practice because it defeats the purpose of data encapsulation (a class hiding its instance variables).

External Global Variables

If you declare a variable, such as int globalVar = 0; outside any method or block, it is known as a global variable- it is accessible to any method or block in that file, even if you declare multiple classes (as we did in the beginning) in one file.

As a matter of fact, declaring the variable as such makes it accessible to any file that #imports it as any other variable- in this sense, it is a global variable.

To declare an external variable specifically (such as within a block, to be used in other blocks), you can use the extern keyword:

extern int extVar;

As a matter of fact, you should not assign extVar a value. The extern keyword tells the compiler that a variable should be external. The keyword is a declaration, not a definition.

Note that you can only define a global variable once, but you can specify any variable as being extern any number of times.

Static Variables

At times there may be a need to declare a variable as global but not external- that is, anything in the file can access the variable, but it cannot be accessed in another file. This is where you would use the static keyword.

Placing a line such as static int allocCounter = 0; outside any method in a file makes such a static variable. One practical application is that you can use this as a “class variable” of sorts—in this case, you would be able to use this variable to keep track of how many instances of a class were allocated, perhaps by overriding the init method:

// Fraction.m
static int instancesCounter = 0;

- (id)init {
  instancesCounter++;
  return [super init];
}

Here, access to instancesCounter is limited to any method within the file.

This post is part of the Learn Objective-C in 24 Days course.


Previous Lesson Next Lesson