I am new to Objective C and am reading a book called "Visual Quickstart Guide: Objective-C" by Steven Holzner, Peachpit Press
In Chapter 6: Object Oriented Programming, there is a section called Using Class Variables where he writes:
You can create class variables for use with
your classes, but there’s a hitch: every object
of that class shares the same variable, so
if one object changes a class variable, that
variable is changed for all objects. You create
class variables with the static keyword.
Class variables are often useful: for example,
you can use a class variable to keep track of
the number of objects of a particular class created
in a program. You’ll do that in this task.
And says to 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:
Cannot declare variable inside @interface or @protocol
Is the book wrong or am I doing something wrong?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…