Obj-C Pointer and Method Syntax Confusion

Discussion in 'Mac Programming' started by rplumii, Dec 6, 2009.

  1. rplumii macrumors newbie

    Joined:
    Sep 6, 2009
    #1
    I had a question regarding method and pointer declarations in Objective C.
    Code:
    -(NSString *) description
    {
    return (@"I'm slightly confused");
    } 
    
    My question about this snippet is whether I am returning a pointer to a NSString or an actual NSString?

    Code:
    NSString *boolString (BOOL yesNo)
    {
    if (yesNo == NO)
    {
    return (@"NO");
    }
     else
    {
    return (@"YES");
    }
    } // boolString
    
    This relates to my first question. In this I seem to be returning an actual NSString instead of the this ,(NSString *), which if I am to understand is a pointer to an NSString. Please correct me if I'm wrong. My second quesiton about this is that if I am returning a string, why is there an asterisk in front of boolString?
     
  2. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2
    You are returning a pointer to an NSString. However, referring to an objective-c object *without* going through a pointer isn't allowed. So we tend to talk about it as just "an NSString".

    @"foo" is an NSString* literal, and the [ ] message sending operator implicitly dereferences the pointer.
     
  3. larkost macrumors 6502a

    Joined:
    Oct 13, 2007
    #3
    The first thing is that you never pass full objects around, only pointers to them. You only pass full things around when they are a "simple" value, that is they take up only one chunk of memory (so an int, char, float, etc...). Things more complicated than that (strings, objects, etc...) you always pass references to (which, behind the scenes are just ints). Part of this is that in passing things you are copying them, and that would get really messy and prohibitive if you were doing with objects.

    Now to make things even weirder, there is a implementation detail in how the compiler deals with static strings like you are using... that is things like @"this". Rather than have a whole bunch of copies for those static strings hanging around in memory, and needing to go through all the trouble of instating them at run-time, the compiler creates a special form of NString for them, and compiles that into a special address. Then any use of the same sequence of characters points at that special address. This creates the weird effect that while the program is running those "static" strings never go away, and never need to be memory managed. So you never have to do [@"this" retain] or [@"this" release], like you would for dynamically created strings.
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    There is no such thing as a "bare" NSString or any object in Objective-C. They all live on the heap and you only refer to them via pointers. Period. The @"" syntax for NSString literals gives you an NSString *. it's special, but it's not a bare NSString.

    Just assume any time you're dealing with an object, it's via a pointer. There are no stack objects, ever. There are structs and pointers to them, which can confuse a bit, but don't worry about those now.

    http://developer.apple.com/mac/libr...tingStrings.html#//apple_ref/doc/uid/20000148

    you can also pass messages to @"" NSString *s like any other NSString object.

    -Lee

    edit: wow. Too slow. Also, a pointer is not an int. Never tell a beginner that. It's an address in memory. It could be wider than an int.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    P.S. This:
    ...won't work. It's not a proper method definition.
     
  6. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #6
    Sure it will. It's a C function, not an ObjC method.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Oops. Sorry.
     
  8. rplumii thread starter macrumors newbie

    Joined:
    Sep 6, 2009
    #8
    Thank you so much. That made everything a lot clearer. I was really having trouble understanding what I was returning.
     

Share This Page