如何获取方法调用所来自的类的名称

时间:2016-02-02 06:20:15

标签: objective-c iphone ipad xcode7

我尝试了一些事情,

  1. id class = [self.navigationController.viewControllers[1] class];
  2. class *Routeview=(class *)self.parentViewController;
  3. 更确切地说,  我有一个类A,它有一个方法method1,我还有两个类B类和C类。所以我想在运行时找出哪个类调用method1 B类或C类。

1 个答案:

答案 0 :(得分:0)

尝试以下:

在您的方法中添加此内容,

NSString *sourceString = [[NSThread callStackSymbols] objectAtIndex:1];
    // Example: 1   UIKit                               0x00540c89 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163
    NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:@" -[]+?.,"];
    NSMutableArray *array = [NSMutableArray arrayWithArray:[sourceString  componentsSeparatedByCharactersInSet:separatorSet]];
    [array removeObject:@""];

NSLog(@"Stack = %@", [array objectAtIndex:0]);
NSLog(@"Framework = %@", [array objectAtIndex:1]);
NSLog(@"Memory address = %@", [array objectAtIndex:2]);
NSLog(@"Class caller = %@", [array objectAtIndex:3]);
NSLog(@"Function caller = %@", [array objectAtIndex:4]);