Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

teguh123

macrumors member
Original poster
Mar 22, 2011
62
0
Code:
- (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]?
 
Last edited by a moderator:
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 ;)
 
It is definitely not just a matter of preference. It is a matter of which is correct or not.

Code:
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

Code:
[self window]

which is window's getter method.

Code:
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.

Code:
[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

Code:
[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.
 
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

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

Code:
NSAutoreleasePool * pool = NSAutoreleasePool.alloc.init;

instead?

hmmmm...

NSAutoreleasePool.new works but NSAutoreleasePool.alloc.init doesn't. What am I missing here?
 
Last edited:
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)
 
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.
 
Last edited:
Where can I learn more about this dot notation.

Pretty much any relatively new Objective-C tutorial.

Can I then write

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

Code:
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.
 
Code:
houseAddress->bedRoom
Go down the street to the house at 427 Maple St. and look in the bedroom.
Code:
house.bedRoom
A large truck just dumped an entire mobile home on your front lawn. Go look in the bedroom of that building.
Code:
self.bedRoom
Go look in your own bedroom.
 
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:mad:"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.
 
Last edited:
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:
Code:
NSMutableArray *_itemsArray;

@property (nonatomic, retain) NSMutableArray *itemsArray;

@synthesize itemsArray = _itemsArray;
 
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.
 
I think I got it now

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

[NSAutoreleasePool alloc] is equivalent with NSAutoreleasePool.alloc

So I would expect
Code:
[[NSAutoreleasePool alloc] init] 

and

NSAutoreleasePool.alloc.init

to be equivalent too. Where do I went wrong?
 
I think I got it now

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

[NSAutoreleasePool alloc] is equivalent with NSAutoreleasePool.alloc

So I would expect
Code:
[[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.
 
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?
 
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
Code:
[[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.
 
Last edited by a moderator:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.