如何将多个参数传递给“scheduledtimerwithtimeinterval:target:target:选择器:userinfo:重复:'

StackOverflow https://stackoverflow.com/questions/9515591

我知道用户信息用于传递参数,但如何传递超过其中一个?

我猜我必须使用一个对象,但是因为我对象来看,我真的不知道这是正确的吗?

谢谢!

有帮助吗?

解决方案

Create a wrapper object, an NSArray or NSDictionary with the multiple objects you need to pass and pass that wrapper object in userInfo. On the receiver retrieve the objects from the wrapper object.

Example code using a NSDictionary for the wrapper:

Calling code:

NSString *obj1 = @"string1"; 
NSString *obj2 = @"string2"; 
NSDictionary *wrapper = [NSDictionary dictionaryWithObjectsAndKeys:obj1, @"Object1", obj2, @"Object2", nil];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFireMethod:) userInfo:wrapper repeats:NO];

Receiving timer code:

- (void)timerFireMethod:(NSTimer*)theTimer {
    NSDictionary *wrapper = (NSDictionary *)[theTimer userInfo];
    NSString * obj1 = [wrapper objectForKey:@"Object1"];
    NSString * obj2 = [wrapper objectForKey:@"Object2"];
    // ...
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top