Static variables of class Objective-C

I'm new to Objective-C and reading a book called "Visual Quickstart Guide: Objective-C" by Stephen Holzner, Press Peachpit Press

Chapter 6: Object Oriented Programming has a section called β€œUsing Class Variables,” where he writes:

You can create class variables for use with your classes, but theres a hitch: every object of this class has the same variable, so if one object modifies a class variable, then the variable changes for all objects. You create class variables with a static keyword. Class variables are often useful: for example, you can use a class variable to track the number of objects created for a particular class in a program. You will do this in this task.

And says enter the following code:

#import <stdio.h> #import <Foundation/NSObject.h> @interface TheClass: NSObject static int count; //error: cannot declare variable inside @interface or @protocol +(int) getCount; @end ... 

This code gives me an error in Xcode 4:

Unable to declare a variable inside @interface or @protocol

Is the book wrong or am I doing something wrong?

+42
objective-c xcode xcode4
May 17 '11 at 19:07
source share
4 answers

You declare a static variable in the implementation file ( .m file). This should work:

 // TheClass.h @interface TheClass : NSObject + (int)count; @end // TheClass.m static int theCount = 0; @implementation TheClass + (int) count { return theCount; } @end 

This is not a class variable per se; Objective-C has no concept of a class variable. However, when combined with a class method to extract this variable, it works similarly to a class variable. However, it is really just a static C variable available for implementing the class.

+92
May 17 '11 at 19:13
source share

I saw one Visual Quickstart Guide about Unix, and it sucked a lot of time. It doesn't seem to be much better, at least not from the sample. The correct way to create a class variable in Objective-C is as follows:

 // Counted.h @interface Counted : NSObject + (NSUInteger) numberOfInstances; @end // Counted.m #import "Counted.h" static NSUInteger instances = 0; @implementation Counted - (id) init { … instances++; … } - (void) dealloc { instances--; } + (NSUInteger) numberOfInstances { return instances; } @end 

This is actually a static variable, not a real class variable. But you should not worry too much about class variables; they are usually a sign that you are doing something wrong. (I simplify a little, but not a lot.)

If you're looking for a decent Objective-C book, read one of them from Apple . Its free and good reading.

+14
May 17 '11 at 19:13
source share

If a class variable requires more trivial initialization, use dispatch_once :

 @interface Foo () + (Foo *)singleton; @end + (Foo *)singleton { static Foo *_singleton; static dispatch_once_t oncePredicate; dispatch_once(&oncePredicate, ^{ _singleton = [[Foo alloc] init]; }); return _singleton; } 
+9
Mar 14 '14 at 15:19
source share

You must declare a variable in the .m file where @implementation is placed. In this way,

 #import "TheClass.h" static int count; @implementation ... @end 

It is important to note that Objective-C does not actually support class variables. But you can simulate them with static variables, as we do.

+6
May 17 '11 at 19:18
source share



All Articles