Mike1690
Mike1690

Reputation: 33

Why aren't these NSString instance variables allocated?

first post here. I was reading through an Objective-C tutorial earlier, and I saw that they had made a couple of NSString instance variables like this:

@implementation MyViewController {
NSString *stringOne;
NSString *stringTwo;
NSString *stringThree;
NSString *stringFour;
NSString *stringFive;
}

And then simply used them in ViewDidLoad like this:

- (void)viewDidLoad
{
[super viewDidLoad];

stringOne = @"Hello.";
stringTwo = @"Goodbye.";
stringThree = @"Can't think of anything else to say.";
stringFour = @"Help...";
stringFive = @"Pheww, done.";
}

How have they done this without instantiating the string? Why does this work? Surely you'd have to do something like stringOne = [NSString stringFromString:@"Hello."]; to properly alloc and init the object before you could simply do stringOne= @"Hello.";.

Sorry if this a dumb question, but I find these little things throw me.

Thanks, Mike

Upvotes: 2

Views: 315

Answers (4)

meth
meth

Reputation: 1885

actually this can be said "syntactic sugar". there are some other type of NS object that can be creatable without allocation or formatting. e.g:

NSNumber *intNumber1 = @42;
NSNumber *intNumber2 = [NSNumber numberWithInt:42];

NSNumber *doubleNumber1 = @3.1415926;
NSNumber *doubleNumber2 = [NSNumber numberWithDouble:3.1415926];

NSNumber *charNumber1 = @'A';
NSNumber *charNumber2 = [NSNumber numberWithChar:'A'];

NSNumber *boolNumber1 = @YES;
NSNumber *boolNumber2 = [NSNumber numberWithBool:YES];

NSNumber *unsignedIntNumber1 = @256u;
NSNumber *unsignedIntNumber2 = [NSNumber numberWithUnsignedInt:256u];

NSNumber *floatNumber1 = @2.718f;
NSNumber *floatNumber2 = [NSNumber numberWithFloat:2.718f];

// an array with string and number literals
NSArray *array1 = @[@"foo", @42, @"bar", @3.14];

// and the old way
NSArray *array2 = [NSArray arrayWithObjects:@"foo", 
                                            [NSNumber numberWithInt:42], 
                                            @"bar", 
                                            [NSNumber numberWithDouble:3.14], 
                                            nil];

// a dictionary literal
NSDictionary *dictionary1 = @{ @1: @"red", @2: @"green", @3: @"blue" };

// old style
NSDictionary *dictionary2 = [NSDictionary dictionaryWithObjectsAndKeys:@"red", @1, 
                                                                       @"green", @2, 
                                                                       @"blue", @3, 
                                                                       nil];

for more information, see "Something wonderful: new Objective-C literal syntax".

Upvotes: 2

Burhanuddin Sunelwala
Burhanuddin Sunelwala

Reputation: 5343

Just remember this basic thing:-

NSString *string = ...

This is a pointer to an object, "not an object"!

Therefore, the statement: NSString *string = @"Hello"; assigns the address of @"Hello" object to the pointer string.

@"Hello" is interpreted as a constant string by the compiler and the compiler itself allocates the memory for it.

Similarly, the statement

NSObject *myObject = somethingElse;

assigns the address of somethingElse to pointer myObject, and that somethingElse should already be allocated and initialised.

Therefore, the statement: NSObject *myObject = [[NSObject alloc] init]; allocates and initializes a NSObject object at a particular memory location and assigns its address to myObject.

Hence, myObject contains address of an object in memory, for ex: 0x4324234.

Just see that we are not writing "Hello" but @"Hello", this @ symbol before the string literal tells the compiler that this is an object and it returns the address.

I hope this would answer your question and clear your doubts. :)

Upvotes: 2

tkanzakic
tkanzakic

Reputation: 5499

From the Apple String Programming Guide:

Creating Strings

The simplest way to create a string object in source code is to use the Objective-C @"..." construct:

NSString *temp = @"Contrafibularity";

Note that, when creating a string constant in this fashion, you should use UTF-8 characters. Such an object is created at compile time and exists throughout your program’s execution. The compiler makes such object constants unique on a per-module basis, and they’re never deallocated. You can also send messages directly to a string constant as you do any other string:

BOOL same = [@"comparison" isEqualToString:myString];

Upvotes: 6

progrmr
progrmr

Reputation: 77193

String constants like @"Hello" are already allocated and initialized for you by the compiler.

Upvotes: 3

Related Questions