是否可以更改 NSTimer 的用户信息?
我有一个 NSTimer
,它的 userInfo
中有一个 NSNumber
fireTimer = [NSTimer scheduledTimerWithTimeInterval:6.0
target:self
selector:@selector(fireTileAddingToColumn:)
userInfo:myNumber
repeats:YES];
在创建 NSTimer
并运行了几次之后有时,我希望能够更改 myNumber
的值并将其反映在 fireTileAddingToColumn:
中,但我没有运气让它发挥作用。有人有什么建议吗?
I have an NSTimer
that has an NSNumber
in its userInfo
fireTimer = [NSTimer scheduledTimerWithTimeInterval:6.0
target:self
selector:@selector(fireTileAddingToColumn:)
userInfo:myNumber
repeats:YES];
After the NSTimer
is created and has run a couple of times, I would like to be able to change the value of myNumber
and have it reflect in fireTileAddingToColumn:
I have not had any luck getting this to work. Does anyone have any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您始终可以将保存 userInfo 的对象传递给计时器:
数据更改将反映在选择器中
You can always pass an object holding the userInfo to the timer:
and the data change will be reflected in the selector
你最好创建一个新的计时器。如果该类不提供用于更改该属性的接口,那么您应该将其视为私有且只读。
在这种情况下,甚至不可能使用 KVC 来完成通常的最终运行:
因为
NSTimer
对于该密钥不符合 KVC。You're best off creating a new timer. If the class doesn't provide an interface for changing that attribute, then you should consider it private and read-only.
It's not even possible in this case to do the usual end run around that, using KVC:
since
NSTimer
is not KVC-compliant for that key.NSTimer 的 userInfo 属性并不是通用的数据存储工具。它的存在是为了让你的计时器能够有一些上下文,这样,如果多个计时器调用相同的操作,计时器的目标就可以区分一个计时器和另一个计时器。
The userInfo property of an NSTimer isn't intended to be a general-purpose data storage facility. It's there so that you can have some context go along with your timer, so that the target of the timer can distinguish one timer from another if multiple timers invoke the same action.
懒人的可变
userInfo
:并访问它:
Lazy people's mutable
userInfo
:And to access it: