gpt4 book ai didi

ios - CGFloat 地板到 NSInteger

转载 作者:可可西里 更新时间:2023-11-01 06:21:32 25 4
gpt4 key购买 nike

在 Xcode 中,编译器提示以下转换:

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

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

一种解决方法是简单地将 CGFloat 转换为截断的 NSInteger,但我想通过显式地板使代码清晰/易于阅读。是否有返回 int 的 flooring 函数?或者其他一些(干净的)方法来做到这一点?

我在“Apple LLVM 6.0 - 编译器标志”下的编译器设置,在“其他 cflags”中,我有 -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

谢谢!

最佳答案

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

要解决此问题,您所要做的就是将 floor(width) 放在括号中像这样:

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

或者将 floor 操作的结果保存到另一个 CGFloat 并将新变量转换为 NSInteger

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

关于ios - CGFloat 地板到 NSInteger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31687285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com