Preston
Preston

Reputation: 25

Symbols in Obj-C

What are all of the symbols in obj-c? Like %@, %d, etc. What do they all mean? Thanks

Upvotes: 0

Views: 393

Answers (5)

These are known as format specifiers. Obj-C has a number of format specifiers; I have mentioned some in the example below, along with the output:

NSString * name=@"XYZ";
NSLog(@"Name=%@",name);

int count=100;
NSLog(@"Count=%d",count);

Float number=555.55;
NSLog(@"number=%f",number);

Output will be:

Name=XYZ
Count=100
number=555.5549

Upvotes: 2

Rollo Konig-Brock
Rollo Konig-Brock

Reputation: 70

They are format specifiers. Each type is for a different type of variable. For instance %i is for int type integers.

int theInteger = 25;
NSLog(@"The integer is = %i", theInteger);

Will print, 'The integer is = 25'. All others are for different types of stored variable. Except for the %@. The %@ specifier means object. Which can be things like NSStrings, NSArray. And pretty much anything subclassed under NSObject.

NSString *stringObject = [[NSString alloc]initWithString@"The String"];
NSlog(@"The string is = %@", stringObject);

You will not understand the latter until you understand objects. I recommend picking up a good book on Obj-C. 'Programming in Objective-C' by Stephen G. Kochan is a good start and will explain objects well.

Upvotes: 1

ohe
ohe

Reputation: 3653

The best response is in the Mac Os X Refrence library.

The big difference between C and Objective C is the %@ symbols. The %@ print as string the return of descriptionWithLocale of an object if available.

Upvotes: 0

miku
miku

Reputation: 187994

%@ and %d are format specifiers.

NSString supports the format characters defined for the ANSI C function printf(), plus @ for any object. If the object responds to the descriptionWithLocale: message, NSString sends that message to retrieve the text representation, otherwise, it sends a description message.

See also: Apple Documentation – FormatStrings.html

Upvotes: 7

Eiko
Eiko

Reputation: 25632

They are called Format Specifiers.

Upvotes: 1

Related Questions