views:

155

answers:

4

i.e. %@ for strings, %f for doubles... I don't know the word for these placeholders but it would be great to have list for how to print booleans and other values.

+3  A: 

It is a normal C format string with the extension of %@ (which prints any NSObject by querying its -description method, not just NSStrings).

You can see an overview in printf manpage

Louis Gerbarg
It is a normal C format string with the exception of the (evil) %n conversion specifier. It's not working in objective-C format strings.
Nikolai Ruhe
+3  A: 

Since NSLog takes a NSString as its argument, it uses the NSString format specifiers. This is virtually identical to the common printf specifiers. Also, the %@ specifier is not limited to NSString objects, but is for any Objective-C objects. The base NSObject class provides a generic description of the object consisting of its class and its address, but many objects will supply information specific to their type, such as the collection classes (NSArray, NSDictionary) which will supply nicely formated dump of their contents. You can provide this for your own objects that you create by overriding -description (see the documentation for more info, including localization capability).

See also: NSString Format Specifiers

johne
"%@" isn't valid for any Objective-C object, only objects that respond to "description". If I were to create my own object deriving from Object (declared in "objc/Object.h"), it won't necessarily be compatible with "%@".
dreamlax
This is true, even though that is what the `NSString Format Specifiers` documentation defines it as. Even adding `description` is not enough if you're building your own root class.
johne
+2  A: 

Also, there's a very nice overview, as well as some tips and tricks, in the most recent "Friday Q&A" posting on Mike Ash's NSBlog blog:

http://www.mikeash.com/?page=pyblog/friday-qa-2009-07-17-format-strings-tips-and-tricks.html

Jim
+1  A: 
JWD