Is there an overview of all codes that can be used inside NSLog()?

早过忘川 提交于 2019-12-24 06:44:36

问题


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.


回答1:


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




回答2:


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




回答3:


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




回答4:


Here is a little snapshot from "Programming in Objective-C 2.0"

alt text http://img361.imageshack.us/img361/1438/picture1rze.png



来源:https://stackoverflow.com/questions/1153003/is-there-an-overview-of-all-codes-that-can-be-used-inside-nslog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!