Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@"%@ in Objective C?

Tags:

objective-c

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#?

like image 760
doo Avatar asked Feb 04 '26 18:02

doo


2 Answers

%@ 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.

like image 56
dreamlax Avatar answered Feb 06 '26 14:02

dreamlax


@"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 *


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!