NSLog() でインスタンスのクラス名を表示する方法。
NSLog(@"%@", NSStringFromClass([self class]));
NSLog() でメソッド名を表示する方法。
NSLog(@"%@", NSStringFromSelector(_cmd));
NSLog() でメソッド名とそのメソッドが属するクラス名を同時に表示する方法。
NSLog(@"%s", __func__);
NSLog(@"%s", __FUNCTION__);
NSLog(@"%s", __PRETTY_FUNCTION__);
NSLog() でソースファイル名を表示する方法。
NSLog(@"%s", __FILE__);
NSLog() でソースファイル内の行番号を表示する方法。
NSLog(@"%d", __LINE__);
NSLog() でコールスタックを表示する方法。
NSLog(@"%@", [NSThread callStackSymbols]);
NSLog() で UIView 階層を表示する方法。
NSLog(@"%@", [self.view recursiveDescription]);
例)
- (void)viewWillAppear:(BOOL)animated
{
NSLog(@"In Objective-C method...");
NSLog(@"%@", NSStringFromClass([self class]));
NSLog(@"%@", NSStringFromSelector(_cmd));
NSLog(@"%s", __func__);
NSLog(@"%s", __FUNCTION__);
NSLog(@"%s", __PRETTY_FUNCTION__);
NSLog(@"%s", __FILE__);
NSLog(@"%d", __LINE__);
NSLog(@"%@", [NSThread callStackSymbols]);
NSLog(@"%@", [self.view recursiveDescription]); // 非公開メソッド
Foo foo;
foo.doSomething();
doIt();
}
void Foo::doSomething()
{
NSLog(@"In C++ function...");
NSLog(@"%s", __func__);
NSLog(@"%s", __FUNCTION__);
NSLog(@"%s", __PRETTY_FUNCTION__);
}
void doIt()
{
NSLog(@"In C function...");
NSLog(@"%s", __func__);
NSLog(@"%s", __FUNCTION__);
NSLog(@"%s", __PRETTY_FUNCTION__);
}
2012-03-14 13:40:26.936 NSLogTest[1779:f803] In Objective-C method...
2012-03-14 13:40:26.939 NSLogTest[1779:f803] ViewController
2012-03-14 13:40:26.940 NSLogTest[1779:f803] viewWillAppear:
2012-03-14 13:40:26.940 NSLogTest[1779:f803] -[ViewController viewWillAppear:]
2012-03-14 13:40:26.941 NSLogTest[1779:f803] -[ViewController viewWillAppear:]
2012-03-14 13:40:26.942 NSLogTest[1779:f803] -[ViewController viewWillAppear:]
2012-03-14 13:40:26.942 NSLogTest[1779:f803] /path/to/file/ViewController.mm
2012-03-14 13:40:26.943 NSLogTest[1779:f803] 51
2012-03-14 13:40:26.946 NSLogTest[1779:f803] (
0 NSLogTest 0x00003157 -[ViewController viewWillAppear:] + 311
1 UIKit 0x000db38f -[UIViewController _setViewAppearState:isAnimating:] + 158
2 UIKit 0x000db5eb -[UIViewController __viewWillAppear:] + 62
.........
23 UIKit 0x00010c65 -[UIApplication _run] + 576
24 UIKit 0x00012626 UIApplicationMain + 1163
25 NSLogTest 0x00002a62 main + 130
26 NSLogTest 0x000029d5 start + 53
)
2012-03-14 13:40:27.164 NSLogTest[1779:f803] >
| ; contentOffset: {0, 0}>
| | >
| | >
2012-03-14 13:40:27.165 NSLogTest[1779:f803] In C++ function...
2012-03-14 13:40:27.168 NSLogTest[1779:f803] doSomething
2012-03-14 13:40:27.169 NSLogTest[1779:f803] doSomething
2012-03-14 13:40:27.169 NSLogTest[1779:f803] void Foo::doSomething()
2012-03-14 13:52:04.307 NSLogTest[1815:f803] In C function...
2012-03-14 13:52:04.308 NSLogTest[1815:f803] doIt
2012-03-14 13:52:04.308 NSLogTest[1815:f803] doIt
2012-03-14 13:52:04.308 NSLogTest[1815:f803] void doIt()
ピンバック: NSLogでいろいろ表示する! | Morizotter Blog