【问题标题】:Cant check if CGPoint is not equal CGPointZero无法检查 CGPoint 是否不等于 CGPointZero
【发布时间】:2013-08-01 17:45:32
【问题描述】:

我在UIView 类中声明了一个CGPoint,在我的viewController 中我尝试检查CGPoint 是否不等于CGPointZero,但我收到此错误:Invalid operands to binary expression ('CGPoint' (aka 'struct CGPoint') and 'CGPoint')

这是 if 语句:

if (joystick.velocity != CGPointZero)

错误指向!=,我不知道为什么它给我一个错误。

joystickUIView 类,CGPoint velocity 声明如下:

@property(nonatomic) CGPoint velocity;

【问题讨论】:

    标签: ios if-statement cgpoint


    【解决方案1】:

    试试这个:

    if (!CGPointEqualToPoint(joystick.velocity, CGPointZero))
    

    解释:CGPoint 实际上是一个结构体。二元操作数(“==”或“!=”)仅用于比较原始值,通常用于比较指针,实际上是表示内存位置的整数。

    由于您有一个结构,而不是对某物的引用,您必须比较结构内的每个值,但幸运的是,苹果已经实现了一个宏,可以在 CGPoint 的情况下为您执行此操作。

    如果你好奇,可以命令点击上面的宏,看看实现:

    __CGPointEqualToPoint(CGPoint point1, CGPoint point2)
    {
      return point1.x == point2.x && point1.y == point2.y;
    }
    

    【讨论】:

    • 你打败了我这个答案:)
    • 现在要试试这个,我认为它是这样的,就像 isEqualToString 方法一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    相关资源
    最近更新 更多