Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling properties' getter method with dot notation

I'm having a bit of difficulties in getting used with Objective-C properties.. I am showing a piece of code just to explain my doubt:

A.h

@interface A : NSObject
@property (nonatomic,getter = isChosen) BOOL chosen;
@end

main.m

A *myClass = [[A alloc]init];

myClass.chosen = YES;
NSLog(@"1. myClass.chosen = %hhd", myClass.chosen);
myClass.chosen = NO;
NSLog(@"2. myClass.chosen = %hhd", myClass.chosen);          
NSLog(@"3. myClass.chosen = %hhd", [myClass isChosen]);      
NSLog(@"4. myClass.chosen = %hhd", myClass.isChosen);

OUTPUT

1. myClass.chosen = 1
2. myClass.chosen = 0
3. myClass.chosen = 0
4. myClass.chosen = 0

Everything is clear for me, apart from the last line of code, where I get the value of the chosen property with myClass.isChosen: I understand the dot syntax myClass.chosen, because the compiler converts it in the message notation syntax [myClass isChosen], but I don't really understand why myClass.isChosen works, or better, I think that again the compiler converts it in the message notation but it seems a little bit weird to me.

I'd like to know if it is considered good practice to call the getter method with the dot notation syntax and if it seems weird only to me.. Obviously this is noticed only when you change the getter name in the property declaration, otherwise NSLog 2 and 4 are the same.

like image 445
Gianni Costanzi Avatar asked Oct 03 '22 00:10

Gianni Costanzi


1 Answers

Dot Syntax Is a Concise Alternative to Accessor Method Calls
As well as making explicit accessor method calls, Objective-C offers an alternative dot syntax to access an object’s properties.

Dot syntax allows you to access properties like this:

NSString *firstName = somePerson.firstName;
somePerson.firstName = @"Johnny";

Dot syntax is purely a convenient wrapper around accessor method calls. When you use dot syntax, the property is still accessed or changed using the getter and setter methods mentioned above:

Getting a value using somePerson.firstName is the same as using [somePerson firstName] Setting a value using somePerson.firstName = @"Johnny" is the same as using [somePerson setFirstName:@"Johnny"] This means that property access via dot syntax is also controlled by the property attributes. If a property is marked readonly, you’ll get a compiler error if you try to set it using dot syntax.

Have a look at this

To read from a variable, dot notation & direct reading are same. In your case, myClass.chosen is same as [myClass isChosen]. here you have just assigned a name to your getter. So instead of calling [myClass chosen], you are calling [myClass isChosen]. Difference comes into the picture when you are assigning to the variable. that is,

[self setVar:foo] is same as self.var=foo but, self->var= foo; is different from [self setVar:foo]
Direct assignment to variable does not leak memory, while self.var invokes accessor methods & hence increases retain count. Let me know if more info needed

Edit

Sorry, I didn't understood you previously.
Dot (.) is not only a shortcut for setter, it's shortcut for getter too. You can use dot for getter too. There is no problem, neither this is bad practice. From Obj-C 2.0 programming guide, "You can use the dot syntax to invoke accessor methods using the same pattern as accessing structure elements. The dot syntax is purely “syntactic sugar”". Note that, it is saying about accessor method, not only setter.

Edit 2
while searching for your answer, i found this.. Thought to share with you..

When you have a member variable on, and your getter for this variable is called isOn then .on and .isOn are two very different kind of things. By using the getter (and probably a setter, too) you will adhere to the "information hiding" promise, whereas by using direct access to the member variables you won't. Cocoa won't enforce those things as it is relying on conventions. It's up to you to decide which way is right for you. Considering convention, you would have to stick to setters and getters - no matter what names you give them, though.

"Overall, I think that this is a rather silly addition to the language since we already had a syntax for sending messages." -Aaron Hillegass (Cocoa Programming for Mac OSX, 3rd. Ed)

Hope this one helps you… :)

like image 72
Prince Agrawal Avatar answered Oct 20 '22 07:10

Prince Agrawal