What's the difference between . and ->

Discussion in 'iPhone/iPad Programming' started by teguh123, Mar 30, 2011.

  1. teguh123, Mar 30, 2011
    Last edited by a moderator: Mar 30, 2011

    macrumors member

    Joined:
    Mar 22, 2011
    #1
    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]?
     
  2. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    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 ;)
     
  3. macrumors 68040

    Joined:
    Apr 22, 2005
    #3
    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.
     
  4. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #4
    I meatn more like
    textLabel.text = @"blah";
    or [textLabel setText:mad:"blah"];

    That's what I meant.
     
  5. teguh123, Mar 30, 2011
    Last edited: Mar 30, 2011

    thread starter macrumors member

    Joined:
    Mar 22, 2011
    #5
    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?
     
  6. macrumors 68040

    Joined:
    Apr 22, 2005
    #6
    Those two are the same, yes, but that is not what the OP asked.
     
  7. macrumors member

    Joined:
    Jun 29, 2009
    #7
    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)
     
  8. robbieduncan, Mar 30, 2011
    Last edited: Mar 30, 2011

    Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    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.
     
  9. macrumors 68040

    Joined:
    Apr 22, 2005
    #9
    Pretty much any relatively new Objective-C tutorial.

    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.
     
  10. macrumors 603

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #10
    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.
     
  11. huskerchad, Mar 30, 2011
    Last edited: Mar 30, 2011

    macrumors regular

    Joined:
    Jul 17, 2002
    #11
    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.
     
  12. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    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;
     
  13. macrumors regular

    Joined:
    Jul 17, 2002
    #13
    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.
     
  14. thread starter macrumors member

    Joined:
    Mar 22, 2011
    #14
    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?
     
  15. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #15
    NSAutoreleasePool's alloc method is not an accessor.

     
  16. thread starter macrumors member

    Joined:
    Mar 22, 2011
    #16
    So that's where I went wrong. . only works for accessor.

    How come new works then?

    NSAutoreleasePool.new seems to work fine
     
  17. macrumors regular

    Joined:
    Jul 17, 2002
    #17
    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?
     
  18. Luke Redpath, Apr 1, 2011
    Last edited by a moderator: Apr 1, 2011

    macrumors 6502a

    Joined:
    Nov 9, 2007
    #18
    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

    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.
     

Share This Page