objective-c - 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.
解决方案
尝试使用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.
}
}];
推荐阅读
- c# - ASP.NET Core:我将如何从用户声明中绑定数据?
- javascript - 有没有办法通过单击 vuejs 中的列表项来更改图标的颜色?
- python - Particle position not being parametrized properly in pyswarms
- python - 管理员中具有 manyToMany InvalidCursorName 的模型
- grafana - 如何及时显示石墨计数器的变化(在grafana中)?
- python - Kivy 的 3D 渲染(猴子旋转)apk 在启动时崩溃
- python - 如何在不删除标题和标题的情况下将 pandas DataFrame 写入现有 Excel 模板?
- mysql - Joining two tables in laravel - how to have second table data as a property of result
- python - 为 django-allauth 覆盖表单 django-crispy-forms 中的电子邮件标题
- javascript - How can i make the Button check if other buttons clicked