PDA

View Full Version : What's the difference between . and ->




teguh123
Mar 30, 2011, 05:57 AM
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

// Add the navigation controller's view to the window and display.
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];

return YES;
}

Look at that code for example, why they use self.window?

Why not self->window?

Why not [self getWindow]?



jnoxx
Mar 30, 2011, 06:31 AM
Try use [ code] tags around your code.
Second of all, it's a mather of preference.
Because, one is a more Objective C 1.0 practice. and the other one is 2.0.
So I think it's a mather of what u want ;)

admanimal
Mar 30, 2011, 06:57 AM
It is definitely not just a matter of preference. It is a matter of which is correct or not.


self.window


is valid Obj-C 2.0 syntax for accessing the window property of self. To be able to do this, window must be declared as a property of whatever class self is (UIApplicationDelegate in this case). This code will ultimately call


[self window]


which is window's getter method.


self->window


is C code for how you would access window if it was a member of a struct pointed to by self. I'm not sure what affect this will have when used on an Obj-C class, but it is definitely not the correct way to do what you are trying to do.


[self getWindow]


is almost the same thing as doing self.window, except you have the name of the accessor wrong. As I pointed out above, it should be


[self window]


as getter methods in Obj-C do not use the word get in their name, but rather just the name of the variable they return.

jnoxx
Mar 30, 2011, 07:00 AM
I meatn more like
textLabel.text = @"blah";
or [textLabel setText:@"blah"];

That's what I meant.

teguh123
Mar 30, 2011, 07:00 AM
Try use [ code] tags around your code.
Second of all, it's a mather of preference.
Because, one is a more Objective C 1.0 practice. and the other one is 2.0.
So I think it's a mather of what u want ;)

Ah,

So self.hi is equivalent with [self hi]

Seems that the former makes much more sense if you are from C++ background :)

Where can I learn more about this dot notation.

Can I then write


NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];



NSAutoreleasePool * pool = NSAutoreleasePool.alloc.init;

instead?


hmmmm...

NSAutoreleasePool.new works but NSAutoreleasePool.alloc.init doesn't. What am I missing here?

admanimal
Mar 30, 2011, 07:01 AM
I meatn more like
textLabel.text = @"blah";
or [textLabel setText:@"blah"];

That's what I meant.

Those two are the same, yes, but that is not what the OP asked.

dutchchilly
Mar 30, 2011, 07:32 AM
As how I understand it; with the dot expression you directly access the variable. With the other expression you're using the (under the hood) getters and setters functions.

I think you can expand/add more logic/code to those getters and setters functions, wich you would bypass if you access the variable directly.

Correct me if I'm wrong
(and excuse my grammar plz)

robbieduncan
Mar 30, 2011, 07:53 AM
As how I understand it; with the dot expression you directly access the variable. With the other expression you're using the (under the hood) getters and setters functions.

I think you can expand/add more logic/code to those getters and setters functions, wich you would bypass if you access the variable directly.

Correct me if I'm wrong
(and excuse my grammar plz)

The . expression uses the getters and setters. You can confirm this by writing your own getters and setters (instead of synthesizing them) and putting NSLog statements in them.

Edit: for confirmation that the dot syntax is simply syntactic sugar see the bottom of page 19 of this PDF (http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjectiveC/ObjC.pdf).

admanimal
Mar 30, 2011, 09:56 AM
Where can I learn more about this dot notation.



Pretty much any relatively new Objective-C tutorial.


Can I then write


NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];



NSAutoreleasePool * pool = NSAutoreleasePool.alloc.init;

instead?


hmmmm...

NSAutoreleasePool.new works but NSAutoreleasePool.alloc.init doesn't. What am I missing here?

No, you can't just substitute dots for methods everywhere. You need to read some kind of beginner's Objective-C material. It is essential to have a precise understanding of what a particular syntax means.

firewood
Mar 30, 2011, 10:23 AM
houseAddress->bedRoom
Go down the street to the house at 427 Maple St. and look in the bedroom.
house.bedRoom
A large truck just dumped an entire mobile home on your front lawn. Go look in the bedroom of that building.
self.bedRoom
Go look in your own bedroom.

huskerchad
Mar 30, 2011, 10:24 AM
As how I understand it; with the dot expression you directly access the variable. With the other expression you're using the (under the hood) getters and setters functions.

I think you can expand/add more logic/code to those getters and setters functions, wich you would bypass if you access the variable directly.

Correct me if I'm wrong
(and excuse my grammar plz)

I believe you're wrong. And I think it's a strong argument against Apple having implemented this stuff. I say this as a full-time C++ developer of many years and someone who generally prefers C++ to Obj-C!

Here's the thing:

self.mystring = @"asdf";

means the same as

[self setMystring:@"asdf"];

In other words, it uses your setter, which may be synthesized or hand-rolled, and may be doing copy or retain for memory management.

Compare what (to most people I think, certainly to most C++ developers) looks like the same thing:

mystring = @"asdf";

No accessor here. Direct access. No retain or copy. Look at those again!

self.mystring = @"asdf";
mystring = @"asdf";

Two very similar statements, with very different effects. This gotcha is why I have stayed far away form the dot stuff in Obj-C.

dejo
Mar 30, 2011, 10:57 AM
Two very similar statements, with very different effects. This gotcha is why I have stayed far away form the dot stuff in Obj-C.

This is why I try to name my instance variables different (but similar) from the property they are for. For example:
NSMutableArray *_itemsArray;

@property (nonatomic, retain) NSMutableArray *itemsArray;

@synthesize itemsArray = _itemsArray;

huskerchad
Mar 30, 2011, 11:51 AM
This is why I try to name my instance variables different (but similar) from the property they are for.

Good practice. I'd actually forgotten you can do that! I'd definitely follow this for any project that is going to use dot notation.

teguh123
Mar 30, 2011, 12:33 PM
I think I got it now

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

[NSAutoreleasePool alloc] is equivalent with NSAutoreleasePool.alloc

So I would expect


[[NSAutoreleasePool alloc] init]

and

NSAutoreleasePool.alloc.init



to be equivalent too. Where do I went wrong?

dejo
Mar 30, 2011, 12:43 PM
I think I got it now

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

[NSAutoreleasePool alloc] is equivalent with NSAutoreleasePool.alloc

So I would expect


[[NSAutoreleasePool alloc] init]

and

NSAutoreleasePool.alloc.init



to be equivalent too. Where do I went wrong?

NSAutoreleasePool's alloc method is not an accessor.

The . expression uses the getters and setters. You can confirm this by writing your own getters and setters (instead of synthesizing them) and putting NSLog statements in them.

Edit: for confirmation that the dot syntax is simply syntactic sugar see the bottom of page 19 of this PDF (http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjectiveC/ObjC.pdf).

teguh123
Mar 31, 2011, 12:49 PM
NSAutoreleasePool's alloc method is not an accessor.

So that's where I went wrong. . only works for accessor.

How come new works then?

NSAutoreleasePool.new seems to work fine

huskerchad
Mar 31, 2011, 01:12 PM
So that's where I went wrong. . only works for accessor.

How come new works then?

NSAutoreleasePool.new seems to work fine

Did you read the PDF that was linked and what it has to say about the subject of calling non-accessor methods by means of dot notation?

Luke Redpath
Apr 1, 2011, 08:20 AM
NSAutoreleasePool's alloc method is not an accessor.

This doesn't matter. You *can* use dot syntax to call any method. But that doesn't mean you should.

Dot syntax should be reserved for accessors or accessor-like methods only (which may or may not be properties).

http://eschatologist.net/blog/?p=160

I think I got it now

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

[NSAutoreleasePool alloc] is equivalent with NSAutoreleasePool.alloc

So I would expect


[[NSAutoreleasePool alloc] init]

and

NSAutoreleasePool.alloc.init



to be equivalent too. Where do I went wrong?

Yes, this is equivalent. But you shouldn't do it. Use dot syntax only for accessing properties and accessor/property-like methods. Use dot syntax accessing exposed state, use brackets for methods that have side-effects.