I've got a doubt about assigning values to properties in Objective-C. I've had a lot of problems of "misteriously missed" values, for example if I have this interface:
// MyClass.h
@interface MyClass : NSObject {
MyOtherClass *myVar;
}
@property (nonatomic, retain) MyOtherClass *myVar;
- (id)initWithValue:(NSString *)val;
And this implementation:
// MyClass.m
@implementation MyClass
@synthesize myVar;
- (id)initWithValue:(NSString *)val {
self = [super init];
if (self != nil) {
/** Do some stuff with val... **/
myVar = [method:val]; // Some method that returns a MyOtherClass value
}
return self;
}
At some point of the execution, the value of myVar disappears, changes or something else... And the solution is to change:
myVar = [method:val]; // Some method that returns a MyOtherClass value
for
self.myVar = [method:val]; // Some method that returns a MyOtherClass value
So... what is the diference between using self or not using it? I mean, it's clear that I've to use it because if not it will cause problems but I don't know why
Thank you in advance