目标 C - 错误:二进制表达式的操作数无效("浮点"和"浮点")



我试图验证除法剩余。下面是我的代码:

NSUInteger myNumber = 2;
//  list is a NSArray
if ((arrayImg.count / ((float) imgPerPage)) % 1 >0)
{
 // do something
}

但是我得到这个错误:

错误:二进制表达式('float'和'float')的操作数无效

如果我这样做:

float result = (arrayImg.count / ((float) imgPerPage));

工作正常,但我不明白为什么我使用%1,我得到错误。

你们谁知道我的代码出了什么问题?

我真的很感激你的帮助

您只能对整数操作数使用模运算符(%),因此首先将浮点表达式转换回整数。而且看起来你在尝试测试奇数/偶数,所以你需要% 2,而不是% 1。所以改变:

if ((arrayImg.count / ((float) imgPerPage)) % 1 >0)

:

if (((int)(arrayImg.count / (float) imgPerPage)) % 2 > 0)

最新更新