Objective C instance variables - Newbie

Posted by Dwayne King on Stack Overflow See other posts from Stack Overflow or by Dwayne King
Published on 2012-11-07T04:42:33Z Indexed on 2012/11/07 5:00 UTC
Read the original article Hit count: 201

Filed under:

OK - so I'm sure my confusion here is just a result of being stuck in a "Java mindset" and not understanding how Obj C differs in this case.

In Java, I can declare a variable in a class, like this, and each instance of that class will have it's own:

MyClass {

  String myVar;

  MyClass() {
    // constructor
  }
}

In Obj C I tried to do the same thing by declaring a variable only in the .m file like this:

#import "MyClass.h"

@implementation MyClass

NSString *testVar;

@end

My expectation here was that this variable has a scope limited to this class. So I created a second class (identical):

#import "MySecondClass.h"

@implementation MySecondClass

NSString *testVar;

@end

What I'm seeing (and has me baffled) is that changing the variable in one class, affects the value seen in the other class. In fact, if I set a breakpoint, and then "Jump to Definition" of the variable, it takes me to th

I've created an extremely small XCode project that demonstrates the problem here

Nothing more humbling than moving to a new language :)

Thanks in advance.

© Stack Overflow or respective owner

Related posts about objective-c