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

zippyfly

macrumors regular
Original poster
Mar 22, 2008
141
0
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"

?
 
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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.