![](http://www.cocoachina.com/cms/uploads/allimg/130701/4196_130701163536_1.jpg)
这里先推荐两篇Xcode相关的文章:
iOS调试 — 基本技巧
本文目录:
1、添加一个特殊的断点
异常断点(Exception breakpoint)
符号断点(Symbolic breakpoint)
2、打印到控制台
使用NSLog打印字符串
使用NSLog打印对象(po)
带条件的打印
在循环里面打印一些东西
3、运行时设置断点
4、调试中播放声音
5、LLDB中有用的一些命令
打印帮助
打印调用栈
打印最基本的内容 (p)
打印对象(po)
打印表达式(expr)
打印线程中的一些东西
正文
下面是非常有用的一些Xcode调试技术(使用断点和LLDB调试器)
1、添加一个特殊的断点
异常断点(Exception breakpoint)
如果添加了异常断点,当程序每次发生了异常,都会被中断。一般用来捕获未知异常。如下示例:
- *** Terminating app due to uncaught exception ’NSRangeException’, reason:
- ’-[__NSCFArray objectAtIndex:]: index (10) beyond bounds (3)
![](http://www.cocoachina.com/cms/uploads/allimg/130701/4196_130701163645_1.png)
符号断点(Symbolic breakpoint)
符号断点可以中断某个函数的调用。
- - [UIViewController viewDidLoad]
- - [__NSCFArray objectAtIndex:]
![](http://www.cocoachina.com/cms/uploads/allimg/130701/4196_130701163720_1.png)
2、打印到控制台
使用NSLog打印字符串
使用断点来替换NSLog代码(或者在运行时添加一个NSLog)——与代码写NSLog的效果相同。
![](http://www.cocoachina.com/cms/uploads/allimg/130701/4196_130701163744_1.png)
使用NSLog打印对象(po)
- NSLog(@"obj: %@", obj);
![](http://www.cocoachina.com/cms/uploads/allimg/130701/4196_130701163819_1.png)
带条件的打印
例如:当aNumber大于10才打印出“str”的内容。
- expr (void)NSLog(@"Ok, print a log: %@", str)"
![](http://www.cocoachina.com/cms/uploads/allimg/130701/4196_130701163855_1.png)
在循环里面打印一些东西
例如,在循环中希望i大于5才开始打印。
- for ( int i=0; i<10; i++ )
- {
- [self self]; // something
- }
使用“ignore”值,并利用下面的代码进行打印:
- expr (void)NSLog(@"Ok, print a log: %@", str)
![](http://www.cocoachina.com/cms/uploads/allimg/130701/4196_130701163957_1.png)
3、运行时设置断点
在运行的时候,根据条件设置断点有时候非常有用。
- breakpoint set -f APViewController.m -l 33
![](http://www.cocoachina.com/cms/uploads/allimg/130701/4196_130701164035_1.png)
4、调试中播放声音
![](http://www.cocoachina.com/cms/uploads/allimg/130701/4196_130701164113_1.png)
5、LLDB中有用的一些命令
当Xcode停留在某个断点时,我们可以通过控制台(console)与
lldb进行交互。
打印帮助
- (lldb) help
打印调用栈(bt)
- (lldb) bt
- * thread #1: tid = 0x1c03, 0x00003146 Debug`-[APViewController callMe:andANumber:](self=0x07187e50, _cmd=0x000038b9, str=0x0715aa40, aNum=38) + 230 at APViewController.m:33, stop reason = breakpoint 3.1
- frame #0: 0x00003146 Debug`-[APViewController callMe:andANumber:](self=0x07187e50, _cmd=0x000038b9, str=0x0715aa40, aNum=38) + 230 at APViewController.m:33
- frame #1: 0x0000304a Debug`-[APViewController viewDidLoad](self=0x07187e50, _cmd=0x005c5a77) + 122 at APViewController.m:16
- frame #2: 0x000f41c7 UIKit`-[UIViewController loadViewIfRequired] + 536
- frame #3: 0x000f4232 UIKit`-[UIViewController view] + 33
- frame #4: 0x000433d5 UIKit`-[UIWindow addRootViewControllerViewIfPossible] + 66
- frame #5: 0x0004376f UIKit`-[UIWindow _setHidden:forced:] + 368
- frame #6: 0x00043905 UIKit`-[UIWindow _orderFrontWithoutMakingKey] + 49
- frame #7: 0x0004c917 UIKit`-[UIWindow makeKeyAndVisible] + 65
- frame #8: 0x00002e1b Debug`-[APAppDelegate application:didFinishLaunchingWithOptions:](self=0x07560750, _cmd=0x005a9c21, application=0x0716a640, launchOptions=0x00000000) + 571 at APAppDelegate.m:28
- frame #9: 0x00010157 UIKit`-[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 266
- frame #10: 0x00010747 UIKit`-[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1248
- frame #11: 0x0001194b UIKit`-[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 805
- frame #12: 0x00022cb5 UIKit`-[UIApplication handleEvent:withNewEvent:] + 1022
- frame #13: 0x00023beb UIKit`-[UIApplication sendEvent:] + 85
- frame #14: 0x00015698 UIKit`_UIApplicationHandleEvent + 9874
- frame #15: 0x01becdf9 GraphicsServices`_PurpleEventCallback + 339
- frame #16: 0x01becad0 GraphicsServices`PurpleEventCallback + 46
- frame #17: 0x01c06bf5 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
- frame #18: 0x01c06962 CoreFoundation`__CFRunLoopDoSource1 + 146
- frame #19: 0x01c37bb6 CoreFoundation`__CFRunLoopRun + 2118
- frame #20: 0x01c36f44 CoreFoundation`CFRunLoopRunSpecific + 276
- frame #21: 0x01c36e1b CoreFoundation`CFRunLoopRunInMode + 123
- frame #22: 0x0001117a UIKit`-[UIApplication _run] + 774
- frame #23: 0x00012ffc UIKit`UIApplicationMain + 1211
- frame #24: 0x00002b22 Debug`main(argc=1, argv=0xbffff3a4) + 130 at main.m:16
- frame #25: 0x00002a55 Debug`start + 53
- (lldb)
打印最基本的内容 (p)
- (lldb) print anInt
打印对象(po)
- (lldb) po anObj
- (lldb) po 0x0715aa40
打印表达式(expr)
- (lldb) expr 5+2
- (lldb) expr aString = @"aNewValue"
打印线程中的一些东西
- (lldb) help frame