LoginSignup
44
42

More than 5 years have passed since last update.

[Objective-C] 呼び出し元メソッドを調べる

Last updated at Posted at 2014-04-09

実際の実装に使っていいかは分かりませんが、呼び出し元を調べる方法を探していてstack overflowで見つけたのでメモしておきます。

// `callStackSymbols`メソッドでコールスタックを得る
NSString *sourceString = [NSThread callStackSymbols][1];
//=> (出力例)1   AppName        0x000bca8a -[AnyViewController viewDidAppear:] + 266

// `NSCharacterSet`クラスを使ってセパレータのセットを作る
NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:@" -[]+?.,"];

// セパレータを使ってスタックの文字列を分解
NSMutableArray *array = [NSMutableArray arrayWithArray:[sourceString componentsSeparatedByCharactersInSet:separatorSet]];

// 空文字を排除
[array removeObject:@""];

// それぞれを出力
NSLog(@"Class caller = %@", array[3]);  // => AnyViewController
NSLog(@"Method caller = %@", array[4]); // => viewDidAppear:
44
42
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
44
42