views:

54

answers:

2

Hi,

I've read somewhere that Objective-C doesn't have class level attributes, but that the same can be achieved by declaring something like this (before the class interface):

static NSInteger initCount;

I'm initializing the variable to zero with the initialize method:

// interface
+ (void) initialize;

// implementation
+ (void) initialize {
   initCount = 0;
}

And incrementing/decrementing when an instance is created/dealloc'd:

- (id) init {
    self = [super init];

    initCount++;

    return self;
}

- (void) dealloc {
    [name release];

    initCount--;

    [super dealloc];
}

But XCode keeps warning me that "'initCount' defined but not used".

Is there any way to solve this, or should I just ignore the warning?

A: 

Do you also have an instance variable named "initCount"? If so, you're referencing this in your init/dealloc methods, and not the global.

Ben Gottlieb
I don't get why this is downvoted. I don't imagine it's particularly likely (because then it would be pretty obvious that there *is* an unused variable with that name), but it's possible. Weirder things have happened. If you're going to downvote, please comment, people — if there's something wrong with an answer, let us know what it is.
Chuck
+6  A: 

It should go in your implementation file, not your interface. If you put it in your header, a separate variable called initCount will be created in each file that imports the header (because a static variable has file scope, and #import textually inserts the contents of the header).

Chuck
Yep, that solves the problem. Thanks.
Tiago
Tiago: Then you should click the checkmark next to this answer to mark it as Accepted. That way, anyone else with the same problem can easily see that you found this to be the correct answer.
Peter Hosey

related questions