Is there any way that I can pass arguments in selector?
example:
I have this method
- (void)myMethod:(NSString*)value1 setValue2:(NSString*)value2{
}
and I need to call this function through a selector passing two arguments.
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(/*my method*/) userInfo:nil repeats:YES];
How can I do this?
You could use the
NSTimermethod:Instead, since an
NSInvocationobject will allow you to pass arguments; anNSInvocationobject is, as the docs define it:Whilst creating an
NSTimerobject using a selector requires the format of the method being:An
NSInvocationallows you to set the target, the selector, and the arguments that you pass in:Where
MyObjectis the class thatmyMethod:setValue2:is declared and implemented on –instanceMethodSignatureForSelector:is a convenience function declared onNSObjectwhich returns anNSMethodSignatureobject for you, to be passed toNSInvocation.Also, to note, with
setArgument:atIndex:, the indices for arguments to be passed to the method set as the selector start at index 2. From the docs: