Possible Duplicate:
(Objective-)C ints always initialized to 0?
I have an interface
@interface MyInterface
{
NSInteger _count;
}
@end
Then in my implementation I am just using is as
if (_count==0)
{
//do somthing
}
_count++;
And it works i.e. the first time around when this is executed the value is in fact 0 even though I never initialized it to be zero.
Is it because the default value of NSInteger is 0?
Assuming you meant to write ==
instead of =
, the answer is that all of the instance variables (ivars) of an Objective-C class get initialized to 0 or nil
when the object gets created. See the question (Objective-)C ints always initialized to 0?.
If you actually wrote if (_count = 0)
with a single =
, then that's not doing what you expected -- it's assigning 0 to _count
, and then testing if it's non-zero (the expression if (x)
tests if x
is non-zero). Since you just assigned 0 to it, it's not non-zero, so the test will always fail.