increment ++正在添加4而不是1个Objective C Xcode

时间:2016-06-15 18:26:20

标签: ios objective-c xcode increment

我刚刚开始为斯坦福大学开设一个纸牌游戏" Machismo" 我有以下代码构建和运行很好,但我的标签在角落里跟踪卡片翻转增量4而不是1.帮助!

#import "CardGameViewController.h"

@interface CardGameViewController ()
@property (weak, nonatomic) IBOutlet UILabel *flipsLabel;
@property (nonatomic) int *flipCount;
@end

@implementation CardGameViewController

- (void) setFlipCount:(int *) flipCount {
    _flipCount = flipCount;
    self.flipsLabel.text = [NSString stringWithFormat:@"Flips: %d", (int) self.flipCount];
}

    enter code here
- (IBAction)touchCardButton:(UIButton *)sender {
    if ([sender.currentTitle length]) { 
        [sender setBackgroundImage:[UIImage imageNamed:@"cardback"] 
                          forState:UIControlStateNormal];
        [sender setTitle:@"" forState:UIControlStateNormal];
    }else {
        [sender setBackgroundImage:[UIImage imageNamed:@"cardfront"]
                          forState:UIControlStateNormal];
        [sender setTitle:@"A♣︎" forState:UIControlStateNormal];
    }
    self.flipCount++;  // << HERE IS THE INCREMENT <<

}


@end

1 个答案:

答案 0 :(得分:4)

您正在递增指针(在您的情况下递增指针的大小和sizeof(int)== 4)并将其转换为int以进行打印。你应该增加一个int。