语义问题:指针和整数之间的比较



我在网上进行了研究,但找不到答案。在Xcode 6上,我有一个错误:

语义问题:指针和整数之间的比较("UILabel*"和"int")

代码是(ViewController.m):

    -(void)enemyMovement {
enemyShip.center = CGPointMake(enemyShip.center.x, enemyShip.center.y + 2);
if (CGRectIntersectsRect(enemyShip.frame, mothersShip.frame)) {
    lives = lives - 1;
    liveString = [NSString stringWithFormat:@"LIVES: %i", lives];
    livesLabel.text = liveString;
    //Stop enemy moving
    [enemyMovementTimer invalidate];
    if (livesLabel > 0) {
        [self positionEnemy];
    }

    if ((livesLabel = 0)) {
        [self gameOver];
    }

    }
}

-(void)gameOver {
[enemyMovementTimer invalidate];
[missleMovementTimer invalidate];
[self performSelector:@selector(replayGame) withObject:nil afterDelay:3];

}

- (void)replayGame {
    score = 0;
    lives = 3;
    scoreString = [NSString stringWithFormat:@"SCORE: 0"];
    liveString = [NSString stringWithFormat:@"LIVES: 3"];
    scoreLabel.text = scoreString;
    livesLabel.text = liveString;
    startButton.hidden = YES;
    friendlyShip.hidden = NO;
    enemyShip.hidden = NO;
    mothersShip.hidden = NO;
    scoreLabel.hidden = NO;
    livesLabel.hidden = NO;
    [self positionEnemy];
}
当整数为"1"时会发生错误,但

当整数为"0"时,不会发生错误,并且生命变为负数,但命令不会针对任一数字发生。该命令是恢复到游戏的开始。我认为问题与生活标签的设置方式有关,我也认为这可能是关于"=="的东西。但我不完全确定我是否试图解决这个问题,但我多次失败。我希望你能帮助我。

提前谢谢你

livesLabel > 0livesLabel对象的内存地址与数字0进行比较。您想要的是将0与标签显示的文本进行比较。比较如下所示:[livesLabel.text intValue] > 0

您正在将标签与数字进行比较。您可能需要重新学习一些基本的编程概念,我无法帮助您,但是UILabel具有text属性,可以使用intValue解析为int。

if (livesLabel > 0) {
    [self positionEnemy];
}

if ((livesLabel = 0)) {
    [self gameOver];
}

我什至不知道你想实现什么,你应该lives比较,而不是标签与数字,就像试图将贴纸与数字进行比较一样,这绝对没有意义。