NSInter是apple推荐用的整形数据类型,在mac64位环境下用打印NSInteger的时候如果用%d,编译器会报警告:

对于32位代码,需要的%d说明符。但是,如果%d说明,得到的64位提示警告用%ld代替:

而如果%ld相匹配的64位大小,32位代码中,会得到一个警告提示%d代替:

  

如何解决呢?

NSInteger的定义是这样的 :

#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif

也就是说64位下是long 类型,十六进制, 32位下是int类型, 8进制;

我的解决方法:

用%i 或者 %zi代替, %zi 是非负整数;

原因:  

%i和%d都是表示有符号十进制整数,但%i可以自动将输入的八进制(或者十六进制)转换为十进制,而%d则不会进行转换。

 
 

在使用诸如NSLog, [NSString stringWithFormat:]之类的函数时,都是基于c/c++风格的字符串格式化工作的.

本来c/c++就没怎么用过,到iphone开发开发时掺合上NS系的对象,格式化输出更是一头的乱.
看了一下Programming Guide for Cocoa的文档,还是有比较详尽的说明的,整理出来备查.

格式定义
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.

其他的补充:

%@                    对象
%d, %i   整数
%u 无符整形
%f 浮点/双字
%x, %X   二进制整数
%o 八进制整数
%zu size_t
%p 指针
%e 浮点/双字 (科学计算
%g 浮点/双字
%s C 字符串
%.*s Pascal字符串
%c 字符
%C unichar
%lld 64位长整数(long long)
%llu 无符64位长整数
%Lf 64位双字
 
 
 参考   
 
 

NSlog警告—— 编译器打印NSInteger类型的更多相关文章

  1. lua中打印所以类型功能实现table嵌套table

    lua中打印所以类型功能实现 本人測试 number.string.bool.nil.table嵌套table.userdata没问题 共享一下有什么问题请拍砖 代码例如以下 cclog = func ...

  2. NSLog的各种打印格式符 和 打印CGRect时用NSStringFromCGRect

    打印CGRect时用NSStringFromCGRect 转载自:http://blog.csdn.net/chenyong05314/article/details/8219270 1. 打印CG开 ...

  3. NSLog打印NSInteger老是有warning

    zSpecifies that a following [...] conversion specifier applies to a size_t or the corresponding sign ...

  4. Objective-C中的占位符,打印BOOL类型数据

    常用的一些占位符: %@:字符串占位符 %d:整型 %ld:长整型 %f:浮点型 %c:char类型 %%:%的占位符 尽管有那么多的占位符,但是好像没有发现BOOL型的数据的占位符,这也是比较纠结的 ...

  5. gcc编译器与基本类型3

    C语言发展史 1969年贝尔实验室 肯尼斯·蓝·汤普逊,丹尼斯·李奇开发了B语言 ->Unix,New B语言,改名C语言83年提出C语言标准 1989年十二月正式通过C语言标准,C89标准 C ...

  6. NSLog设置不打印

    在调试应用程序的时候经常需要进行打印需要的信息,但是当打印的地方多了之后在真机上跑应用程序就会相应的慢很多,输出语句多了之后会在很大程序上影响应用程序的性能.这里我们可以定义一个宏来控制是否输出调试信 ...

  7. NSLog的各种打印格式符和打印CGRect相关结构体

    1.打印CG开头的数据类型,如CGRect,CGSize等方法 1.1 打印CGRect : NSLog(@"%@", NSStringFromCGRect(someCGRect) ...

  8. NSLog用法,打印日志

    要输出的格式化占位:   %@ 对象 %d, %i 整数 %u   无符整形 %f 浮点/双字 %x, %X 二进制整数 %o 八进制整数 %zu size_t %p 指针 %e   浮点/双字 (科 ...

  9. 解决model 里 NSInteger类型

    #import "CJGWCListModel.h" @implementation CJGWCListModel - (NSInteger)goods_number{ if (_ ...

随机推荐

  1. The top 100 papers Nature explores the most-cited research of all time.

    The top 100 papers Nature explores the most-cited research of all time. The discovery of high-temper ...

  2. unity 基础学习 transform

    unity  基础学习   transform 1.unity采用的是右手坐标系,X轴右手为+,Y轴向上为+,Z轴朝里为+; 但是我们从3D MAX中导入模型之后,发现轴向并没有遵从这个原理, 其实是 ...

  3. hdu 5139 Formula

    http://acm.hdu.edu.cn/showproblem.php?pid=5139 思路:这道题要先找规律,f(n)=n!*(n-1)!*(n-2)!.....1!;  不能直接打表,而是离 ...

  4. Flesch Reading Ease(模拟)

    http://poj.org/problem?id=3371 终于遇到简单一点的模拟题了.不过本人真心没有耐心读题目... 它的大致意思就是给一段合法的文章,求出这段文章的单词数,句子数,音节数,按照 ...

  5. POJ-1151-Atlantis(线段树+扫描线+离散化)[矩形面积并]

    题意:求矩形面积并 分析:使用线段树+扫描线...因为坐标是浮点数的,因此还需要离散化! 把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,用col表示该区间有多少个下边,sum代表该区 ...

  6. 类似与fiddler的抓包工具 burp suite free edition

    burp suite free edition

  7. 最小生成树——[HAOI2006]聪明的猴子

    题目:[HAOI2006]聪明的猴子 描述: [题目描述] 在一个热带雨林中生存着一群猴子,它们以树上的果子为生.昨天下了一场大雨,现在雨过天晴,但整个雨林的地表还是被大水淹没着, 猴子不会游泳,但跳 ...

  8. 《A First Course in Probability》-chaper4-离散型随机变量-负二项分布

    基于我们最为熟悉的离散型分布——二项分布,我们能够衍生出很多别的分布列,对于之前介绍过的几何分布,我们赋予其的含义是:某个事件成功的概率是p,在n次独立重复实验中恰好成功一次的概率是多少.顺着这层含义 ...

  9. Jenkins 二:邮件配置

    默认邮件的配置 假设管理员邮箱是 user1@domain1.com,密码是pw1. 1. 打开“系统管理”-> “系统设置”. 2. 找到“Jenkins Location”-> “系统 ...

  10. drp用户管理完成后,asp.net与java的一个简单比较

    DRP视频断断续续看了有一个月的时间了,跟着视频进行,从需求到设计,到现在的编码实现,跟之前用asp.net做系统步调一致,都遵守软件设计的规范,一步步来进行.尤其是编码实现,越来越感觉java与as ...