timdisney
timdisney

Reputation: 5337

What does @"some string" mean in objective-c?

I'm just starting out with iphone development and ran across some example code that used @"somestring"

someLabel.txt = @"string of text";

Why does the string need the '@'? I'm guessing it's some kind of shortcut for creating an object?

Upvotes: 9

Views: 629

Answers (3)

J.Biard
J.Biard

Reputation: 315

Just an interesting side note... NSString literals created by using the @"..." notation are not autoreleased. They essentially just hang around until your program terminates.

Just a caution that if you want to maintain control over whether or not this object gets released (freed) down the road you may want to consider using something like:

[NSString stringWithString:@"..."]; 

...instead. This would create an autoreleased version of the same string that will be freed from memory next time the "autorelease pool is drained".

Just food for thought.

Cheers-

Upvotes: -3

Robert Harvey
Robert Harvey

Reputation: 180788

In Objective-C, the syntax @"foo" is an immutable, literal instance of NSString.

Upvotes: 11

cobbal
cobbal

Reputation: 70733

It creates an NSString object with that string as opposed to the standard c char* that would be created without the '@'

Upvotes: 15

Related Questions