1. 程式人生 > >Objective-c中的佔位符,列印BOOL型別資料

Objective-c中的佔位符,列印BOOL型別資料

常用的一些佔位符:

%@:字串佔位符

%d:整型

%ld:長整型

%f:浮點型

%c:char型別

%%:%的佔位符

儘管有那麼多的佔位符,但是好像沒有發現BOOL型的資料的佔位符,這也是比較糾結的地方,看了一下別人是怎麼解決這個問題的

[cpp] view plaincopyprint?
  1. BOOL studyBool = YES;  
  2.        NSLog(@"列印BOOL型資料%@",[email protected]"YES":@"NO");//列印BOOL型資料YES
  3.        NSLog(@"列印BOOL型資料%d",studyBool);
    //列印BOOL型資料1
  4.        BOOL alsoBool = NO;  
  5.        NSLog(@"列印BOOL型資料%@",[email protected]"YES":@"NO");//列印BOOL型資料NO
  6.        NSLog(@"列印BOOL型資料%d",alsoBool);//列印BOOL型資料0


詳細介紹:**********************************************************

%@:             Objective-C物件,印有字串返回descriptionWithLocale:如果於的話,或描述相反.CFTypeRef工作物件,返回的結果的CFCopyDescription功能.(這個翻譯有問題建議按照自己的理解方式理解)。

%%:             為'%'字元;

%d,%D,%i:   為32位整型數(int);

%u,%U:        為32位無符號整型數(unsigned int);

%hi:   為有符號的16位整型數(short);

%hu:  為無符號的16位整型數(unsigned shord);

%qi:   為有符號的64位整型數(long long);

%qu:  為無符號的64位整型數(unsigned long long);

%x:    為32位的無符號整型數(unsigned int),列印使用數字0-9的十六進位制,小寫a-f;

%X:    為32位的無符號整型數(unsigned int),列印使用數字0-9的十六進位制,大寫A-F;

%qx:   為無符號64位整數(unsigned long long),列印使用數字0-9的十六進位制,小寫a-f;

%qX:   為無符號64位整數(unsigned long long),列印使用數字0-9的十六進位制,大寫A-F;

%o,%O:   為32位的無符號整數(unsigned int),列印八進位制數;

%f:      為64位的浮點數(double);

%e:      為64位的浮點數(double),列印使用小寫字母e,科學計數法介紹了指數的增大而減小;

%E:      為64位的浮點數(double),列印科學符號使用一個大寫E介紹指數的增大而減小;

%g:      為64位的浮點數(double),用%e的方式列印指數,如果指數小於4或者大於等於精度,那麼%f的風格就會有不同體現;

%G:      為64位的浮點數(double),用%E的方式列印指數,如果指數小於4或者大於等於精度,那麼%f的風格就會有不同體現;

%c:       為8位的無符號字元%c(unsigned char),通過列印NSLog()將其作為一個ASCII字元,或者,不是一個ASCII字元,八進位制格式\ddd或統一標準的字元編碼的十六進位制格式\udddd,在這裡d是一個數字;

%C:       為16位Unicode字元%C(unichar),通過列印NSLog()將其作為一個ASCII字元,或者,不是一個ASCII字元,八進位制格式\ddd或統一標準的字元編碼的十六進位制格式\\udddd,在這裡d是一個數字;

%s:       對於無符號字元陣列空終止,%s系統中解釋其輸入編碼,而不是別的,如utf-8;

%S:       空終止一系列的16位Unicode字元;

%p:       空指標(無效*),列印十六進位制的數字0-9和小寫a-f,字首為0x;

%L:       在明確規定的長度下,進行修正,下面的一批資料a,A,e,E,f,F,g,G應用於雙精度長整型的引數;

%a:       為64位的浮點數(double),按照科學計數法列印採用0x和一個十六進位制數字前使用小寫小數點p來介紹指數的增大而減小;

%A:       為64位的浮點數(double),按照科學計數法列印採用0X和一個十六進位制數字前使用大寫字母小數點P界掃指數的增大而減小;

%F:       為64位的浮點數(double),按照十進位制表示法進行列印;

%z:       修改說明在%z長度以下d,i,o,u,x,X適用於某一指定型別的轉換或者適用於一定尺寸的整數型別的引數;

%t:       修改說明在%t長度以下d,i,o,u,x,X適用於某一指定型別或一定尺寸的整數型別的轉換的引數;

%j:       修改說明在%j長度以下d,i,o,u,x,X適用於某一指定型別或一定尺寸的整數型別的轉換的引數。

英文文件

格式定義
The format specifiers supported by the NSString formatting methods and CFString formatting functions follow the IEEE printf specification; the specifiers are summarized in Table 1. Note that you can also use the “n$” positional specifiers such as %[email protected] %2$s. For more details, see the IEEE printf specification. You can also use these format specifiers with the NSLog function.

Table 1 Format specifiers supported by the NSString formatting methods and CFString formatting functions
定義 說明
%@ Objective-C object, printed as the string returned by descriptionWithLocale: if available, or description otherwise. Also works with CFTypeRef objects, returning the result of the CFCopyDescription function.
%% ‘%’ character
%d, %D, %i Signed 32-bit integer (int)
%u, %U Unsigned 32-bit integer (unsigned int)
%hi Signed 16-bit integer (short)
%hu Unsigned 16-bit integer (unsigned short)
%qi Signed 64-bit integer (long long)
%qu Unsigned 64-bit integer (unsigned long long)
%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
%qx Unsigned 64-bit integer (unsigned long long), printed in hexadecimal using the digits 0–9 and lowercase a–f
%qX Unsigned 64-bit integer (unsigned long long), printed in hexadecimal using the digits 0–9 and uppercase A–F
%o, %O Unsigned 32-bit integer (unsigned int), printed in octal
%f 64-bit floating-point number (double)
%e 64-bit floating-point number (double), printed in scientific notation using a lowercase e to introduce the exponent
%E 64-bit floating-point number (double), printed in scientific notation using an uppercase E to introduce the exponent
%g 64-bit floating-point number (double), printed in the style of %e if the exponent is less than –4 or greater than or equal to the precision, in the style of %f otherwise
%G 64-bit floating-point number (double), printed in the style of %E if the exponent is less than –4 or greater than or equal to the precision, in the style of %f otherwise
%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. %s interprets its input in the system encoding rather than, for example, UTF-8.
%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
%L Length modifier specifying that a following a, A, e, E, f, F, g, or G conversion specifier applies to a long double argument
%a 64-bit floating-point number (double), printed in scientific notation with a leading 0x and one hexadecimal digit before the decimal point using a lowercase p to introduce the exponent
%A 64-bit floating-point number (double), printed in scientific notation with a leading 0X and one hexadecimal digit before the decimal point using a uppercase P to introduce the exponent
%F 64-bit floating-point number (double), printed in decimal notation
%z Length modifier specifying that a following d, i, o, u, x, or X conversion specifier applies to a size_t or the corresponding signed integer type argument
%t Length modifier specifying that a following d, i, o, u, x, or X conversion specifier applies to a ptrdiff_t or the corresponding unsigned integer type argument
%j Length modifier specifying that a following d, i, o, u, x, or X conversion specifier applies to a intmax_t or uintmax_t argument

平臺依賴
Mac OS X uses several data types—NSInteger, NSUInteger,CGFloat, and CFIndex—to provide a consistent means of representing values in 32- and 64-bit environments. In a 32-bit environment, NSInteger and NSUInteger are defined as int and unsigned int, respectively. In 64-bit environments, NSInteger and NSUInteger are defined as long and unsigned long, respectively. To avoid the need to use different printf-style type specifiers depending on the platform, you can use the specifiers shown in Table 2. Note that in some cases you may have to cast the value.

Table 2 Format specifiers for data types
型別 定義 建議
NSInteger %ld or %lx Cast the value to long
NSUInteger %lu or %lx Cast the value to unsigned long
CGFloat %f or %g %f works for floats and doubles when formatting; but see below warning when scanning
CFIndex %ld or %lx The same as NSInteger
pointer %p %p adds 0x to the beginning of the output. If you don’t want that, use %lx and cast to long.
long long %lld or %llx long long is 64-bit on both 32- and 64-bit platforms
unsigned long long %llu or %llx unsigned long long is 64-bit on both 32- and 64-bit platforms

The following example illustrates the use of %ld to format an NSInteger and the use of a cast.

1
2
NSInteger i = 42;
printf("%ld\n"(long)i);

In addition to the considerations mentioned in Table 2, there is one extra case with scanning: you must distinguish the types for float and double. You should use %f for float, %lf for double. If you need to use scanf (or a variant thereof) with CGFloat, switch to double instead, and copy the double to CGFloat.

1
2
3
4
CGFloat imageWidth;
double tmp;
sscanf (str, "%lf"&tmp);
imageWidth = tmp;

It is important to remember that %lf does not represent CGFloat correctly on either 32- or 64-bit platforms. This is unlike %ld, which works for long in all cases.