SundayMonday
SundayMonday

Reputation: 19737

Member variable declared in .m file keeps its value between instances of the class

I have the follow implementation file for MyClass:

BOOL myBool;

@implementation MyClass

// ...

- (void) someMethod {
    myBool = YES;
}

@end

It seems like myBool will be YES for every instance of MyClass after someMethod is called on just one instance of MyClass. However if I define myBool like this it has a unique value for each instance of MyClass:

@interface MyClass ()
@property (nonatomic) BOOL myBool;
@end

What is the difference between the above two "member variable" syntaxes?

Upvotes: 0

Views: 82

Answers (1)

Vladimir
Vladimir

Reputation: 170849

The difference is that in 1st case it is not member variable, it is global variable so it naturally persists its value between multiple instances of your class.

If you want to declare ivar in class implementation file you can do the following:

@implementation MyClass{
   BOOL myBool;
}
...

Upvotes: 4

Related Questions