Does @"abcef" mean a temp string object is created?

Discussion in 'Mac Programming' started by zippyfly, Aug 18, 2009.

  1. zippyfly macrumors regular

    Joined:
    Mar 22, 2008
    #1
    In code

    Code:
    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
        NSString *myStr = @"ABCDEFG";
    	
    	NSLog(@"Length: %i", [myStr length]);
    	
    	NSLog(@"Pointer: %p\n\n", myStr);
    
    	myStr = @"Hi";
    	
    	NSLog(@"Length: %i", [myStr length]);
    
    	NSLog(@"Pointer: %p", myStr);
    
    	[pool drain];
        return 0;
    }
    
    Does myStr get assigned a POINTER to temporarily created string object, which gets created when we use the @ in front of the delimited text?

    i.e., because myStr would hold a pointer to a string object, I am wondering what the @ operator does to create that object? I would like to understand what is going on in the background with this assignment, since in other cases:

    Code:
    	int x = 50;
    	int *xPtr;
    	
    	xPtr = &x;
    	
    	NSLog(@"*xPrt: %i", *xPtr);
    
    But in the string case, it's not *myStr = @"String"

    ?
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
  3. zippyfly thread starter macrumors regular

    Joined:
    Mar 22, 2008
  4. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #4
    Just to clarify, the @ symbol in Objective-C isn't an operator like * or &. It's just a special character that Objective-C likes to use to help it parse special keywords or NSString literals. For example, the @ in @property doesn't really operate on something called property- it just helps form a special keyword.
     
  5. zippyfly thread starter macrumors regular

    Joined:
    Mar 22, 2008
    #5
    Thanks for reminder; I understand that (Obj-C being a superset semi-preprocessor on top of ANSI C) for keywords but also overlooked that it applies to the string literals, until you reminded me.
     

Share This Page