比较CGPoint和NSNumber

时间:2011-07-31 05:06:49

标签: iphone objective-c xcode

这是我的第一篇文章,所以要善待:)

我四处寻找解决方案,但似乎无法找到解决方案。问题可能是以谷歌将返回有用结果的方式提出问题。

所以,我有一个NSMutableArray(称为boardColCoords),我有CGPoint(称为touchLocation)。我想比较两者,以便我可以将UIImageView位置捕捉到boardColCoords数组中的正确坐标。

以下是相关代码:

UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];

NSNumber *boardSquareX = [boardColCoords objectAtIndex:i];

if (touchLocation.x - boardSquareX <= 12)
{
}

我知道12最终必须改变,但我只想让减法先行。我得到的具体错误是:

二进制表达式的无效操作数('CGFloat'(又名'浮动')和“NSNumber *)。

3 个答案:

答案 0 :(得分:8)

NSNumber是一个包装器,您必须使用floatValue之类的内容“提取”它的值,以便进行比较:

if (touchLocation.x - [boardSquareX floatValue] <= 12)

答案 1 :(得分:2)

要从NSNumber获取浮点值,您可以

[boardSquareX floatValue];

答案 2 :(得分:1)

您需要从boardSquareX中提取原始数字。

if (touchLocation.x - [boardSquareX floatValue] <= 12)
{
}
相关问题