Objective-c中的占位符,打印BOOL类型数据

常用的一些占位符:

%@:字符串占位符

%d:整型

%ld:长整型

%f:浮点型

%c:char类型

%%:%的占位符

尽管有那么多的占位符,但是好像没有发现BOOL型的数据的占位符,这也是比较纠结的地方,看了一下别人是怎么解决这个问题的

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

objective-c常见类型%z相关推荐

  1. C++求复数的角度_高考数学一轮复习33,复数,常见类型及解题策略

    [考试要求] 1.通过方程的解,认识复数: 2.理解复数的代数表示及其几何意义,理解两个复数相等的含义: 3.掌握复数代数表示式的四则运算,了解复数加.减运算的几何意义. [知识梳理] 2.复数的几何 ...

  2. AI芯片:几种常见类型的AI芯片

    引入   近些年来,深度学习网络不断兴起.数据运算量越来越大,对处理器并行处理能力要求越来越高.传统的通用处理器CPU受摩尔定律的影响,已经不能满足大规模的数据处理了.故而,专用AI处理器的设计再一次 ...

  3. 动态规划常见类型总结

    本文针对动态规划的常见类型进行总结.虽说总结的是动态规划,但顺便把递推也放了进来.严格来说,递推不属于动态规划问题,因为动态规划不仅有递推过程,还要有决策(即取最优),但广义的动态规划是可以包含递推的 ...

  4. 越不服务器显示异常什么原因,计算机异常问题都有哪些常见类型

    故障与异常问题都是程序员需要长期关注的一个问题,下面我们就通过案例分析来了解一下,计算机异常问题都有哪些常见类型. 1.句柄泄漏 句柄泄漏是进程在调用系统文件之后,没有释放已经打开的文件句柄.一般句柄 ...

  5. 四部门联合印发《常见类型移动互联网应用程序必要个人信息范围规定》

    为贯彻落实<中华人民共和国网络安全法>关于"网络运营者收集.使用个人信息,应当遵循合法.正当.必要的原则""网络运营者不得收集与其提供的服务无关的个人信息&q ...

  6. LCD液晶显示屏常见类型和接口

    LCD液晶显示屏常见类型和接口 一.屏幕类型 1. 背光源分类 2. 光电特性分类 二.常用接口 1. VGA(D-SUB) 2. DVI 3. HDMI 4. RGB 5. MIPI 6. LVDS ...

  7. 采购订单管理的四种常见类型

    采购管理对于任何成功的企业都至关重要.如果你的企业没有统一的采购管理流程,那么你可能无法对你的采购进行解释,并可能犯下代价高昂的采购错误.采购订单或许是有效采购管理流程中最重要的部分.让我们来看看采购 ...

  8. 常见类型移动互联网应用程序必要个人信息范围规定(快速解读)

    第一条 为了规范移动互联网应用程序(App)收集个人信息行为,保障公民个人信息安全,根据<中华人民共和国网络安全法>,制定本规定. 第二条 移动智能终端上运行的App存在收集用户个人信息行 ...

  9. 互联网营销活动常见类型

    本篇从宏观的角度来梳理一下互联网营销活动常见类型.不管是电商行业,还是现在比较火的互联网金融行业,各个行业只要有需要消费者的,都会有活动的支持. - 1. 常见活动类型 - 2. 业务逻辑 - 3. ...

最新文章

  1. Java并发- 读写锁中的性能之王:StampedLock
  2. BciPy: 一款基于Python用于BCI研究的开源软件
  3. WebService系列(三)--创建自己的WebService
  4. CST normalize S-parameter to given impedance
  5. iOS开发-缓存图片到沙盒--思路
  6. python matplotlib_Python の Matplotlib
  7. 平衡二叉树中需要旋转的情况
  8. OpenCV颜色转换和皮肤检测
  9. 有限维线性空间过两点有且只有一条直线的证明
  10. HDU Problem 1285 确定比赛名次【拓扑排序】
  11. php镜像站群_【镜像站群系统】2020最新单域名PHP网站克隆镜像网站程序源码
  12. Goland中time.Timer and time.Ticker
  13. 如何适配处理iphoneX底部的横条 ?(转)
  14. 召回率(Recall)
  15. 【SQLServer】用SQL语句更改数据库名,表名,列名
  16. FPGA 中的有符号数乘法
  17. 公司网站制作需要多少钱?
  18. 【随笔】学会拒绝别人,聪明地说不,学会独处
  19. 设定ssh方式登陆路由器的方法
  20. 如何设计杀手级的物联网产品或服务?

热门文章

  1. R语言学习笔记(二) 逻辑运算符
  2. 无法访问计算机请检查名称的拼写,win10系统访问共享文件夹提示“请检查名称的拼写”的修复方案...
  3. 使用 KubeSphere 和极狐GitLab 打造云原生持续交付系统
  4. 夕阳红旅游团第四周报告
  5. H5浪漫告白气球表白
  6. 快消品行业S2B2C电子商务网站提升供应链效率,加速行业整合
  7. HTTP之URL详解
  8. 基于Python+Django+Vue+MYSQL的社团管理系统
  9. 基于OpenCV的形状检测
  10. 对话 Apache 巨咖 - 如何做好一个开源项目?