首页 > 白话Objective-C > 白话Objective-C之NSLog

白话Objective-C之NSLog

NSLog函数用打印信息到控制台,比如一些错误信息或者日志信息

void NSLog (
NSString *format,

);

其中string format主要如下三张表所示:

Table 1  Format specifiers supported by the NSString formatting methods and CFString formatting functions

Specifier Description
%@ Objective-C object, printed as the string returned by descriptionWithLocale: if available, or description otherwise. Also works with CFTypeRef objects, returning the result of theCFCopyDescription function.
%% ‘%’ character.
%d Signed 32-bit integer (int).
%u Unsigned 32-bit integer (unsigned int).
%x Unsigned 32-bit integer (unsigned int), printed in hexadecimal using the digits 0–9 and lowercase a–f.
%X Unsigned 32-bit integer (unsigned int), printed in hexadecimal using the digits 0–9 and uppercase A–F.
%o Unsigned 32-bit integer (unsigned int), printed in octal.
%f 64-bit floating-point number (double).
%c 8-bit unsigned character (unsigned char), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format \\ddd or the Unicode hexadecimal format \\udddd, where d is a digit.
%C 16-bit Unicode character (unichar), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format \\ddd or the Unicode hexadecimal format \\udddd, where d is a digit.
%s Null-terminated array of 8-bit unsigned characters. Because the %s specifier causes the characters to be interpreted in the system default encoding, the results can be variable, especially with right-to-left languages. For example, with RTL, %s inserts direction markers when the characters are not strongly directional. For this reason, it’s best to avoid %s and specify encodings explicitly.
%S Null-terminated array of 16-bit Unicode characters.
%p Void pointer (void *), printed in hexadecimal with the digits 0–9 and lowercase a–f, with a leading 0x.

Table 2  Length modifiers supported by the NSString formatting methods and CFString formatting functions

Length modifier  Description
h Length modifier specifying that a following d, o, u, x, or X conversion specifier applies to a short or unsigned short argument.
l Length modifier specifying that a following d, o, u, x, or X conversion specifier applies to a long or unsigned long argument.
ll Length modifiers specifying that a following d, o, u, x, or X conversion specifier applies to a long long or unsigned long long argument.
L Length modifier specifying that a following a, A, e, E, f, F, g, or G conversion specifier applies to a long double argument.
z Length modifier specifying that a following d, o, u, x, or X conversion specifier applies to a size_t or the corresponding signed integer type argument.

 

Table 3  Format specifiers for data types。Platform Dependencies

Type Format specifier  Considerations
NSInteger %ld or %lx Cast the value to long.
NSUInteger %lu or %lx Cast the value to unsigned long.

(转载本站文章请注明出处 www.helloitworks.com ,请勿用于任何商业用途)

  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.