Como passo mais de um parâmetro para o seletor em 'ScheduledTimerWithTimeInterVal: Target: selector: userInfo: repete:'
-
14-11-2019 - |
Pergunta
Eu sei que as informações do usuário são usadas para passar um parâmetro, mas como passo mais do que um desses?
Eu estou supondo que tenho que usar um objeto, mas como sou bastante novo para Objective-C Eu realmente não sei se isso é certo e como ir sobre isso?
Obrigado!
Solução
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"];
// ...
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow