Advertisements

Extension 12: 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:

  • @protected: The default case, the variable can be accessed by methods in the defining class and all subclasses.
  • @private: The instance variable can be accessed by instance methods in the defining class, but subclasses can not.
  • @public: Any class can access the variable, provided that the header of the defining class is imported, as usual.
  • @package: Any class within the same framework can access the variable. For example, a @package variable defined in NSString could be accessed by any other class in the Foundation framework, but nowhere else.

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 {
  extern int instancesCounter;
  instancesCounter++;
  return [super init];
}

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

Advertisements
Leave a comment

2 Comments

  1. I like looking through an article that will make people think.
    Also, thanks for permitting me to comment!

    Reply
  1. Learn Objective-C in 24 Days « Programming for iOS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Advertisements
  • Welcome

    My goal is to make CupsOfCocoa into a beautiful source for beginners to the iPhone platform to get started. Subscribe below for more, and stay tuned!

  • Contact Me

    If you need to contact me for any reason, feel free to send me an email.
  • The Giving Spirit

    If you've found this site helpful, would you consider donating a little sum? Any amount is appreciated...Thanks so much!

  • Roadmap

  • Enter your email address to follow this blog and receive notifications of new posts by email.

    Join 222 other followers

  • Back to the Past

    March 2011
    S M T W T F S
    « Feb   Apr »
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
  • Time Machine

  • You count!

    • 621,662 views
  • Worldwide Stats

    free counters
  • Advertisements
%d bloggers like this: