What is the difference between declaring an ivar within an @interface versus putting a variable within an @implementation in a .m file?
As far as I know, putting a variable declaration inside the @implementation is no different from putting it outside the implementation: It's not an ivar, it's just a variable declared at file scope.
One use is for declaring the equivalent of C++ static members. For example:
@implementation MyClass
static int s_count = 0;
- (id)init {
if ((self = [super init]))
++s_count;
return self;
}
- (void)dealloc {
--s_count;
[super dealloc];
}
Assuming init is your only initializer, then s_count will contain the total number of instances of MyClass that are active.