是否可以更改 NSTimer 的用户信息?

发布于 2024-12-18 20:05:05 字数 578 浏览 2 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(4

可是我不能没有你 2024-12-25 20:05:06

您始终可以将保存 userInfo 的对象传递给计时器:

@interface Holder
@property id data;
-(id) initWithData: (id) data;
@end

// implement it

Holder *holder = [[Holder alloc] initWithData:myNumber];
fireTimer = [NSTimer scheduledTimerWithTimeInterval:6.0 target:self
             selector:@selector(fireTileAddingToColumn:) holder
              repeats:YES];

[Holder setData: myNumber2];

数据更改将反映在选择器中

You can always pass an object holding the userInfo to the timer:

@interface Holder
@property id data;
-(id) initWithData: (id) data;
@end

// implement it

Holder *holder = [[Holder alloc] initWithData:myNumber];
fireTimer = [NSTimer scheduledTimerWithTimeInterval:6.0 target:self
             selector:@selector(fireTileAddingToColumn:) holder
              repeats:YES];

[Holder setData: myNumber2];

and the data change will be reflected in the selector

遗失的美好 2024-12-25 20:05:06

你最好创建一个新的计时器。如果该类不提供用于更改该属性的接口,那么您应该将其视为私有且只读。

在这种情况下,甚至不可能使用 KVC 来完成通常的最终运行:

[timer setValue:newNumber forKey:@"userInfo"];

因为 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:

[timer setValue:newNumber forKey:@"userInfo"];

since NSTimer is not KVC-compliant for that key.

萌︼了一个春 2024-12-25 20:05:06

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.

走野 2024-12-25 20:05:06

懒人的可变userInfo

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:6.0 
                                         target:self 
                                       selector:@selector(fireTileAddingToColumn:) 
                                       userInfo:@{@"mutable": [@{@"key": <value>} mutableCopy] 
                                        repeats:YES];

并访问它:

timer.userInfo[@"mutable"][@"key"] = <newValue>;

Lazy people's mutable userInfo:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:6.0 
                                         target:self 
                                       selector:@selector(fireTileAddingToColumn:) 
                                       userInfo:@{@"mutable": [@{@"key": <value>} mutableCopy] 
                                        repeats:YES];

And to access it:

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