How to know which method the selector has done in Objective-C

advertisements

I have really simple example, but I can't understand how I could get information about the method that call selector inside it. For example:

-(void)methodOne {
   [self performSelector:@selector(methodTwo:)];
}
-(void)methodTwo:(id)sender {
   //How to know which method performed selector???
}

I think this example is pretty simple, I just need to understand this, again, the question is, how could I know from methodTwo: which method perform selector, so from which method I called methodTwo:.

Thanks in advance!


If you do want to know which method calls your methodTwo, you can look at call stack. Look at this ...

-(void)methodOne {
  [self performSelector:@selector(methodTwo:)];
}

-(void)methodTwo:(id)sender {
  NSLog( @"%@", [NSThread callStackSymbols][1]);
  NSLog( @"%@", [NSThread callStackSymbols][2]);
}

... and the log output (simplified) is ...

0x014ef671 -[NSObject performSelector:] + 62
0x00002307 -[CMAppDelegate methodOne] + 55

... callStackSymbols returns NSArray with call stack symbols, so, you can look at index 1 to get method which did you call your method. As you can see, there's NSObject's performSelector and it's because you called methodTwo via performSelector. If you change your methodOne in this way ...

-(void)methodOne {
  [self methodTwo:nil];
}

... the output is ...

0x000022fe -[CMAppDelegate methodOne] + 62
0x000025d6 -[CMAppDelegate application:didFinishLaunchingWithOptions:] + 374

... that's it. Your question is answered, but I dunno why do you want this. You probably don't want to know which method exactly did call your method, but which object initiated this call chain. An example is button - you can have one method to handle more buttons and there's sender argument to distinguish between these buttons as @stavash pointed out.