歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Objective-C中的占位符,打印BOOL類型數據

常用的一些占位符:
%@:字符串占位符
%d:整型
%ld:長整型
%f:浮點型
%c:char類型
%%:%的占位符

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

 BOOL studyBool = YES;
        NSLog(@"打印BOOL型數據%@",studyBool?@"YES":@"NO");//打印BOOL型數據YES
        NSLog(@"打印BOOL型數據%d",studyBool);//打印BOOL型數據1
       
        BOOL alsoBool = NO;
        NSLog(@"打印BOOL型數據%@",alsoBool?@"YES":@"NO");//打印BOOL型數據NO
        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 %1$@ %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.

Copyright © Linux教程網 All Rights Reserved