New to Objective-C, and i am basically from c++ background. I am learning now objective-c and would like to get confirmation of what i understood is write or wrong?. Kindly advise.
I have the following class:
@interface Test: NSObject
{
int instance1;
}
@property int instance1;
- (void) sayHello;
@end
Class 'Test' has a instance variable instance1
. If the member function ie: sayHello wants to access the variable, it has to happen through getter/setter functions. So, There are two ways to get it :
- User can define.
- We can get the help from the compiler?. How?. declare the same variable as a property, and synthesize it, the the compiler gets the code of getter/setter for us for that particular variable.
So, Untimately, getter/setter is the only way to access the variable in the method implementation, ie. both self.instance1 = 100;
and instance1 = 100
need getter/setter.
Having missed both 1. and 2., there is no way to access the instance1
variable.
Also, instance1
is a pubic variable can can be accessed outside of the class with object instance.
Test *t = [[ Test alloc] init];
t.instance1 = 200;
Questions:
- Is there any way to make
instance1
is "private", so that I can not access the instance variable outside the class? - Is there anything wrong in my understanding?