CGFloat楼到NSInteger

时间:2015-07-28 21:18:29

标签: ios objective-c iphone xcode

在Xcode中,编译器抱怨以下演员:

CGFloat width = 5.6f; NSInteger num = (NSInteger)floor(width);

说“从'double'类型的函数调用转换为非匹配类型'NSInteger'(又名'int')

一种解决方法是简单地将CGFloat强制转换为NSInteger进行截断,但我希望通过显式地板使代码清晰易读。是否有返回int的地板功能?或者其他一些(干净的)这样做的方式?

我的编译器设置在“Apple LLVM 6.0 - Compiler Flags”下,在“Other C Flags”中,我有 -O0 -DOS_IOS -DDEBUG = 1 -Wall -Wextra -Werror -Wnewline-eof -Wconversion -Wendif -labels -Wshadow -Wbad-function-cast -Wenum-compare -Wno-unused-parameter -Wno-error = deprecated

谢谢!

2 个答案:

答案 0 :(得分:6)

好的,正如你提到严格的编译器设置,我再次尝试并找到了解决方案。 编译器警告是因为你试图将floor函数转换为NSInteger值而不是返回值。

要解决这个问题,你所要做的就是将括号(宽度)放在括号中像这样:

NSInteger num = (NSInteger) (floor(width));

或将楼层操作的结果保存到另一个CGFloat并将新变量强制转换为NSInteger

CGFloat floored = floor(width);
NSInteger num = (NSInteger) floored;

答案 1 :(得分:4)

使用MyNode表示浮点数。所以floorf()

更多信息CGFloat-based math functions?