Reputation: 91
I was following a tut and found a line of code like @"%@ button pressed."
. I'm pretty sure the relevant part is the %@, but is the first @ an escape sequence or what?
Anyways, searching symbols doesn't go well in any search engine so I thought I'd ask. I think the %@ is like {0} in C#?
Upvotes: 3
Views: 196
Reputation: 38343
I found this page which might help - http://www.yetanotherchris.me/home/2009/6/22/objective-c-by-example-for-a-c-developer.html
It seems that you are on the right track.
Upvotes: 1
Reputation: 21
I'm still fairly new to the language, but it looks like the @ specifies that the variable being passed/created is an NSObject, or a compiler directive.
As mentioned above, if you use it like this:
@"someText"
you're instantiating an NSString object, and setting the text of that object to someText. If you look at a good ol' C-style format specifier such as:
..."This is some text, and this is a float: %f", myFloat);
You're creating some text and telling the compiler to put the floating point string representation of myFloat into the string. %@ is a format specifier, just like %f, %d, %c, %s and any other format specifier you're used to. However, if you use %@ as follows:
... "This is some text, and this is an object:%@", myObject];
What you're doing is (I believe) telling the compiler that myObject is an object, and that you want it to include the output of the description method (ie. [myObject description]) in the string that you're creating.
Upvotes: 0
Reputation: 95355
%@
is a format specifier. Functions such as NSLog
and methods such as +stringWithFormat:
will replace %@
with the description of the provided Objective-C or Core Foundation object argument.
For example:
NSString *myName = @"dreamlax";
NSLog (@"My name is: %@", myName);
This will log the output "My name is: dreamlax"
. See here for more information format specifiers.
The initial @
symbol at the beginning of the string tells the compiler to create a static instance of an NSString
object. Without that initial @
symbol, the compiler will create a simpler C-style string. Since C-style strings are not Objective-C objects you cannot add them to NSArray
or NSDictionary
objects, etc.
Upvotes: 9
Reputation:
@"some string" means this is an NSString literal.
The string as show in @"CupOverflowException", is a constant NSString object. The @ sign is used often in Objective-C to denote extentions to the language. A C string is just like C and C++, "String constant", and is of type char *
Upvotes: 2