开发者

Symbols in Obj-C

What are all of the symbols in obj-c? Like %@, %d, etc开发者_如何学运维. What do they all mean? Thanks


%@ and %d are format specifiers.

  • see also http://www.opengroup.org/onlinepubs/009695399/functions/printf.html

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


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


They are called Format Specifiers.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜