首页 > 解决方案 > Check if Variable has changed in Obj C

问题描述

How do you repeatedly check whether a variable has changed?

Currently I'm using a hook class from a game engine to access the score like so:

 float score = ScoreController::scores().points.current();

The function is only called once from the AppDelegate, so I need a method to continuously check if the score then changes after I find out what it is, but I'm unsure how to approach this?

I'm thinking the logic would be:

 //store current score (as above)
     //if score increases (not sure how to check)
          //do something

Thanks for your help.

标签: objective-c

解决方案


尝试使用NSTimer.

NSTimeInterval timeInterval = 1; //How frequently to check for the score change.
CGFloat score = ScoreController::scores().points.current();
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval repeats:YES block:^(NSTimer *timer) {
     CGFloat newScore = ScoreController::scores().points.current();

    if (score != newScore) {
        //Score changed. Call a delegate or something.
    }
}];

推荐阅读