Assuming I have the following class.
@interface MyObj : NSObject{
int age;
}
-(void) setAge: (int) anAge;
-(int) returnAge;
@end
@implementation MyObj
-(void) setAge:(int) anAge{
age = anAge;
}
-(int) returnAge{
return age;
}
If I then want to assign a value to the age variable I could use the class method to do the following in main.
MyObj *newObj = [MyObj new];
[newObj setAge:18];
However from messing around i've noticed that I seem to be able to directly access the ivar using the . notation. I get no warning or error with this.
MyObj *newObj = [MyObj new];
newObj.age = 20;
The end result appears to be same for both but I feel like I shouldn't be using the . notation to directly access ivars like this.
Should I be using getters and setters for all ivars or is directly accessing them like this ok?
Thanks