如何使 BOOL 在短时间内具有值,然后在 Objective-C 中更改

发布于 2024-10-03 04:25:41 字数 166 浏览 0 评论 0原文

我有一个在 if 语句中使用的 Bool 。该变量一开始为 NO,然后我输入 if 语句,如果 bool 值为 NO,则该语句执行。然后我在 if 语句末尾将值更改为 YES,这样它就不会再次执行,标准的东西。

我想要做的是在最后以某种方式更改该变量“是”,但在大约五秒后将其更改回“否”。那怎么办呢?

I have a Bool that I am using in an if statement. The variable starts out as NO, then I enter the if statement, which executes if the bool's value is NO. Then I change the value to YES at the end of the if statement so it won't execute again, standard stuff.

What I want to do is somehow change that variable YES at the end, but have it change back to NO after about five seconds. How can that be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

总以为 2024-10-10 04:25:41

您可以使用 NSTimer

[NSTimer scheduledTimerWithTimeInterval:5.0 
                                 target:self 
                               selector:@selector(resetSomeBoolean:) 
                               userInfo:nil 
                                repeats:NO];


- (void) resetSomeBoolean:(NSTimer *) timer {
   self.someBoolean = NO;
}

另外,请确保此属性 (someBoolean) 是atomic

You can use NSTimer:

[NSTimer scheduledTimerWithTimeInterval:5.0 
                                 target:self 
                               selector:@selector(resetSomeBoolean:) 
                               userInfo:nil 
                                repeats:NO];


- (void) resetSomeBoolean:(NSTimer *) timer {
   self.someBoolean = NO;
}

Also, make sure that this property (someBoolean) is atomic.

甜点 2024-10-10 04:25:41

也许设置类似:

... your code ...
myBool = YES; // set some senteniel.

[NSTimer scheduledTimerWithTimeInterval:5.0 
                                 target:self 
                               selector:@selector(resetMyBool:) 
                               userInfo:nil 
                                repeats:NO];
return;
}

添加类似的内容

- (void) resetMyBool:(NSTimer*)meTimer {
      myBool = NO;
}

并向您的班级 。但是你确定你需要这样做吗 - 听起来很容易获得竞争条件。

谢谢,

Dw。

Perhaps set something like:

... your code ...
myBool = YES; // set some senteniel.

[NSTimer scheduledTimerWithTimeInterval:5.0 
                                 target:self 
                               selector:@selector(resetMyBool:) 
                               userInfo:nil 
                                repeats:NO];
return;
}

and add something like

- (void) resetMyBool:(NSTimer*)meTimer {
      myBool = NO;
}

to your class. But are you sure you need to do this - sounds easy to get a race condition.

Thanks,

Dw.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文