objective-c常见类型%z
常用的一些占位符:
%@:字符串占位符
%d:整型
%ld:長(zhǎng)整型
%f:浮點(diǎn)型
%c:char類型
%%:%的占位符
盡管有那么多的占位符,但是好像沒有發(fā)現(xiàn)BOOL型的數(shù)據(jù)的占位符,這也是比較糾結(jié)的地方,看了一下別人是怎么解決這個(gè)問題的
BOOL studyBool = YES;NSLog(@"打印BOOL型數(shù)據(jù)%@",studyBool?@"YES":@"NO");//打印BOOL型數(shù)據(jù)YESNSLog(@"打印BOOL型數(shù)據(jù)%d",studyBool);//打印BOOL型數(shù)據(jù)1BOOL alsoBool = NO;NSLog(@"打印BOOL型數(shù)據(jù)%@",alsoBool?@"YES":@"NO");//打印BOOL型數(shù)據(jù)NONSLog(@"打印BOOL型數(shù)據(jù)%d",alsoBool);//打印BOOL型數(shù)據(jù)0詳細(xì)介紹:**********************************************************
%@: ? ? ? ? ? ? Objective-C對(duì)象,印有字符串返回descriptionWithLocale:如果于的話,或描述相反.CFTypeRef工作對(duì)象,返回的結(jié)果的CFCopyDescription功能.(這個(gè)翻譯有問題建議按照自己的理解方式理解)。
%%: ? ? ? ? ? ? 為'%'字符;
%d,%D,%i: ? 為32位整型數(shù)(int);
%u,%U: ? ? ? ?為32位無符號(hào)整型數(shù)(unsigned int);
%hi: ? 為有符號(hào)的16位整型數(shù)(short);
%hu: ?為無符號(hào)的16位整型數(shù)(unsigned shord);
%qi: ? 為有符號(hào)的64位整型數(shù)(long long);
%qu: ?為無符號(hào)的64位整型數(shù)(unsigned long long);
%x: ? ?為32位的無符號(hào)整型數(shù)(unsigned int),打印使用數(shù)字0-9的十六進(jìn)制,小寫a-f;
%X: ? ?為32位的無符號(hào)整型數(shù)(unsigned int),打印使用數(shù)字0-9的十六進(jìn)制,大寫A-F;
%qx: ? 為無符號(hào)64位整數(shù)(unsigned long long),打印使用數(shù)字0-9的十六進(jìn)制,小寫a-f;
%qX: ? 為無符號(hào)64位整數(shù)(unsigned long long),打印使用數(shù)字0-9的十六進(jìn)制,大寫A-F;
%o,%O: ? 為32位的無符號(hào)整數(shù)(unsigned int),打印八進(jìn)制數(shù);
%f: ? ? ?為64位的浮點(diǎn)數(shù)(double);
%e: ? ? ?為64位的浮點(diǎn)數(shù)(double),打印使用小寫字母e,科學(xué)計(jì)數(shù)法介紹了指數(shù)的增大而減小;
%E: ? ? ?為64位的浮點(diǎn)數(shù)(double),打印科學(xué)符號(hào)使用一個(gè)大寫E介紹指數(shù)的增大而減小;
%g: ? ? ?為64位的浮點(diǎn)數(shù)(double),用%e的方式打印指數(shù),如果指數(shù)小于4或者大于等于精度,那么%f的風(fēng)格就會(huì)有不同體現(xiàn);
%G: ? ? ?為64位的浮點(diǎn)數(shù)(double),用%E的方式打印指數(shù),如果指數(shù)小于4或者大于等于精度,那么%f的風(fēng)格就會(huì)有不同體現(xiàn);
%c: ? ? ? 為8位的無符號(hào)字符%c(unsigned char),通過打印NSLog()將其作為一個(gè)ASCII字符,或者,不是一個(gè)ASCII字符,八進(jìn)制格式\ddd或統(tǒng)一標(biāo)準(zhǔn)的字符編碼的十六進(jìn)制格式\udddd,在這里d是一個(gè)數(shù)字;
%C: ? ? ? 為16位Unicode字符%C(unichar),通過打印NSLog()將其作為一個(gè)ASCII字符,或者,不是一個(gè)ASCII字符,八進(jìn)制格式\ddd或統(tǒng)一標(biāo)準(zhǔn)的字符編碼的十六進(jìn)制格式\\udddd,在這里d是一個(gè)數(shù)字;
%s: ? ? ? 對(duì)于無符號(hào)字符數(shù)組空終止,%s系統(tǒng)中解釋其輸入編碼,而不是別的,如utf-8;
%S: ? ? ? 空終止一系列的16位Unicode字符;
%p: ? ? ? 空指針(無效*),打印十六進(jìn)制的數(shù)字0-9和小寫a-f,前綴為0x;
%L: ? ? ? 在明確規(guī)定的長(zhǎng)度下,進(jìn)行修正,下面的一批數(shù)據(jù)a,A,e,E,f,F,g,G應(yīng)用于雙精度長(zhǎng)整型的參數(shù);
%a: ? ? ? 為64位的浮點(diǎn)數(shù)(double),按照科學(xué)計(jì)數(shù)法打印采用0x和一個(gè)十六進(jìn)制數(shù)字前使用小寫小數(shù)點(diǎn)p來介紹指數(shù)的增大而減小;
%A: ? ? ? 為64位的浮點(diǎn)數(shù)(double),按照科學(xué)計(jì)數(shù)法打印采用0X和一個(gè)十六進(jìn)制數(shù)字前使用大寫字母小數(shù)點(diǎn)P界掃指數(shù)的增大而減小;
%F: ? ? ? 為64位的浮點(diǎn)數(shù)(double),按照十進(jìn)制表示法進(jìn)行打印;
%z: ? ? ? 修改說明在%z長(zhǎng)度以下d,i,o,u,x,X適用于某一指定類型的轉(zhuǎn)換或者適用于一定尺寸的整數(shù)類型的參數(shù);
%t: ? ? ? 修改說明在%t長(zhǎng)度以下d,i,o,u,x,X適用于某一指定類型或一定尺寸的整數(shù)類型的轉(zhuǎn)換的參數(shù);
%j: ? ? ? 修改說明在%j長(zhǎng)度以下d,i,o,u,x,X適用于某一指定類型或一定尺寸的整數(shù)類型的轉(zhuǎn)換的參數(shù)。
英文文檔
格式定義
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.
| 定義 | 說明 |
| %@ | 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 |
平臺(tái)依賴
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.
| 類型 | 定義 | 建議 |
| 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.
總結(jié)
以上是生活随笔為你收集整理的objective-c常见类型%z的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springboot 模块引用_Spri
- 下一篇: 全球及中国胶粘剂和密封胶基材行业产销现状