ios学习之路(知识点)Values of type 'NSInteger' should not be used as format arguments...
2015-01-15 16:07
435 查看
在使用format arguments的时候,遇到了如题的编译器 warning, 当然Xcode也提供了Fix-it来解决。但作为一个合格的程序者,原理是一定要去探个究竟的,起码这点好奇心如果都没有,那在工作的路上一定会跪掉的。
不废话了,简单如下的demo,就可以重现这个问题。
果断去了SO查看,得到解答,跟度娘上所有的答案都很一致 (但是仍不能解决我的疑问)
http://stackoverflow.com/questions/16075559/why-does-an-nsinteger-variable-have-to-typecasted-to-type-long
首先来看NSInteger的定义(如下),在64位系统中NSInteger是long, 32位中是int。所以"%d"在64位上当然是不足以够位长,这里修改为"%ld"合情合理。
然后Xcode Fix-it推荐做法是下面这样。
然后我尝试去掉后面的long 转换,也编译通过。因为如果是64位,那number不需要转换,如果是32位,那number int值是可以自动转化为long的,那么Xcode为什么要推荐在后面转换呢? 不得其解,有知道的朋友告诉我,谢谢
不废话了,简单如下的demo,就可以重现这个问题。
NSInteger number = 5; NSLog(@"%d", number); [NSString stringWithFormat:@"%d", number];
果断去了SO查看,得到解答,跟度娘上所有的答案都很一致 (但是仍不能解决我的疑问)
http://stackoverflow.com/questions/16075559/why-does-an-nsinteger-variable-have-to-typecasted-to-type-long
首先来看NSInteger的定义(如下),在64位系统中NSInteger是long, 32位中是int。所以"%d"在64位上当然是不足以够位长,这里修改为"%ld"合情合理。
#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
然后Xcode Fix-it推荐做法是下面这样。
[NSString stringWithFormat:@"%ld", (long)number];
然后我尝试去掉后面的long 转换,也编译通过。因为如果是64位,那number不需要转换,如果是32位,那number int值是可以自动转化为long的,那么Xcode为什么要推荐在后面转换呢? 不得其解,有知道的朋友告诉我,谢谢
[NSString stringWithFormat:@"%ld", number];
相关文章推荐
- iOS之整型转换警告Values of type 'NSInteger' should not be used as format arguments;
- values of type NSInteger should not be used as format arguments; 关于Xcode中烦人的32位与64位警告处理方法.
- iOS 整型转换警告"NSInteger' should not be used as format arguments; add an explicit cast to 'long' inste"
- [BS-15] Values of type 'NSInteger' should not be used as format arguments
- SonarQube-Java规范之Atomic-".equals()" should not be used to test the values of "Atomic" classes
- 错误整理:Unable to add module to the current project as it is not of packaging type 'pom'
- Solaris 10 如何解决:/usr/include/sys/siginfo.h:259: error: 'ctid_t' is used as a type, but is not
- Why the "volatile" type class should not be used
- 执行 maven 命令 报错Unable to add module to the current project as it is not of packaging type 'pom'
- warning: the 'gets' function is dangerous and should not be used.的解决办法
- 'Basic' attribute type should not be a persistence entity/a container
- warning: the 'gets' function is dangerous and should not be used.的解决办法
- Linux下开发warning:"the `gets' function is dangerous and should not be used"
- [Visual Studio 2010]如何解决错误CoClass 'xx' can be used only as one of its applicable interfaces
- warning: the ‘gets' function is dangerous and should not be used
- The requested list key 'map' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or peopl
- The project could not be deployed to the 'localhost' server because of the following connectivity problems.
- 错误:error: ISO C++ does not permit 'wxScrolledWindow::sm_eventTable' to be defined as 'MyClass::sm_eventTable'
- The base class includes the field '...', but its type (...) is not compatible with the type of control (...)
- mysql 数据库字段为datetime类型时默认值为:0000-00-00 00:00:00 查询时异常 java.sql.SQLException:Value '0000-00-00' can not be represented as java.sq