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

时间:2015-04-11 16:05:44

标签: ios objective-c

我在线研究过,无法找到答案。在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'错误不会发生,生命会变成负数,但任何一个数字都不会发生命令。该命令将恢复到游戏的开始。我认为这个问题与生活标签的设置方式有关,我也认为这可能是关于' =='。但我不确定我是否已经尝试解决这个问题,但我已经多次失败了。我希望你能帮助我。

提前谢谢

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

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



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

我甚至无法告诉你要实现的目标,你应该比较lives而不是标签与数字相比,就像试图将贴纸与数字进行比较一样,这绝对没有意义