I am having trouble to understand the concept of private instance variables in Objective-C:
Let’s assume I have a class:
@interface Dog : NSObject
and two declared selectors
- (void)setSomeString:(NSString *)_someString;
- (NSString *)someString;
in the Dog.m implementation file I declare a private instance variable:
@interface Dog()
{
NSString *someString;
}
in the main method of the program I create a new dog object:
Dog *myDog = [[Dog alloc] init];
Why is it possible to do something like this out of the main method?
myDog.someString = @"Yoda";
I would expect the someString-variable to be private and only accessible by its setter
[myDog setSomeString:@"Yoda"];
When you use dot-syntax you are actually calling method
setSomeString, the difference is just in syntax, not in meaning 🙂Check Apple documentation about sending a message to an object