iOS开发调试技巧(一)
2016-06-30 17:50
417 查看
在iOS开发中调试是必不可少的,同时也是必须学会的技能,重要性不言而喻。今天我们就来初步了解下一些常用技巧。暂时使用的是OC语言。
此文也可以进入我的简书阅读
当程序运行到断点处时会停止运行,只要在显示行数的相对应的地方点击一下就添加了一个端点。
![](http://upload-images.jianshu.io/upload_images/1981026-4086d273ed977ead.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
添加的端点可以在如下图的地方查看。
![](http://upload-images.jianshu.io/upload_images/1981026-c87d6e59c452ffab.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
程序运行到断点处,可以看到如下图显示的参数
![](http://upload-images.jianshu.io/upload_images/1981026-70455e01ced699d9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在断点处右击按如下图选择
![](http://upload-images.jianshu.io/upload_images/1981026-36cd952c80cf2d70.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在弹出框中设置你想中断程序的条件
![](http://upload-images.jianshu.io/upload_images/1981026-e9b949de7e051e73.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
下图就是程序中断之前打印出的结果
![](http://upload-images.jianshu.io/upload_images/1981026-cf300723efbc5b7a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
还可以通过设置Ignore的参数,忽略前n次的断点运行,程序会在第n+1 次的时候中断
![](http://upload-images.jianshu.io/upload_images/1981026-35c215ac8869f1b7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
下图是运行2次后中断结果
![](http://upload-images.jianshu.io/upload_images/1981026-30be122ea36bd2c5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
还可以通过添加 Action 来进行更多的配置,下图是查看断点所在处的方法调用的次数, %B 是断点所在的方法名, %H是断点调用的次数,注意:选中 Options
![](http://upload-images.jianshu.io/upload_images/1981026-90365e7cfad5235d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
下图为打印的结果
![](http://upload-images.jianshu.io/upload_images/1981026-ff7e0ecf4255f319.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
添加Action中的 Sound,会让程序有节奏的调用,不回马上执行完,在调用的同时可以听到方法调用的声音
![](http://upload-images.jianshu.io/upload_images/1981026-b7b55911e29771cf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
下图为添加异常断点的操作地方,首先按图中上面的尖头点击进入这个模式,再点击左下角 + 按钮添加
![](http://upload-images.jianshu.io/upload_images/1981026-0acc22a62abee9d0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
下图为添加后的结果
![](http://upload-images.jianshu.io/upload_images/1981026-5dc1f13ffe7ba181.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
添加异常断点后,当程序遇到异常crash时,程序就会在出现异常代码的地方终止。如果没有打异常断点程序会在main.m中终止。下图为数组越界的一个示范
![](http://upload-images.jianshu.io/upload_images/1981026-b951cd9514a57962.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
创建方式如下图
![](http://upload-images.jianshu.io/upload_images/1981026-c5e70a2a1ded3e74.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
符号断点可以在你指定的[类名 方法名]中中断执行,如下图添加,此时程序会在ViewController类的loadData中中断
![](http://upload-images.jianshu.io/upload_images/1981026-e5ffb04cfe3f38fc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
还可以只添加方法,此时程序会在整个工程中只要遇到该方法就会中断
![](http://upload-images.jianshu.io/upload_images/1981026-49e8a850fe6cf590.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
下图为程序中断地方
![](http://upload-images.jianshu.io/upload_images/1981026-b81096f5be7742b5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在iOS开发中,把已经release但还没完全消失的对象称作僵尸对象,对已经release对的对象再次释放,就会发生异常。使用ARC后,对象释放产生的异常已经很少,但是偶尔还是会出现。
开启僵尸对象模式后就能快速定位异常位置。开启的方式如下:Product-->Scheme-->Edit Scheme,再按照下图勾选就可以了。
![](http://upload-images.jianshu.io/upload_images/1981026-da6b3a03c0e458f7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Xcode中使用的llvm编译器,而lldb是llvm中的调试器,我们可以使用一些简单的命令进行调试,拿上面那个for循环作为测试代码
![](http://upload-images.jianshu.io/upload_images/1981026-182c360ba26301ce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
程序运行后中止在断点处,在下方打印出区域使用po命令、print命令打出相对应的信息,如下图
![](http://upload-images.jianshu.io/upload_images/1981026-569ef84afac24870.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
NSLog打印信息一般是初学者最喜欢的打印,通过打印出的信息产看程序运行的结果,但是打印的信息较少,本身NSLog效率也比较低,使用下面的这个宏能够打印出所在类名、所在方法名、行号、具体时间。
#import"ViewController.h"
#define DebugLog(fmt, ...) do { \
fprintf(stderr,"<%s> %s Line %d\n", [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String],\
__PRETTY_FUNCTION__, __LINE__); \
(NSLog)((fmt), ##__VA_ARGS__); \
fprintf(stderr,"----------\n"); \
} while (0)
@interfaceViewController()
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
[selfloadData];
}
- (void)loadData
{
intcount =10;
while(count <15) {
DebugLog(@"count = %d",count);
count ++;
}
}
![](http://upload-images.jianshu.io/upload_images/1981026-8d14efaa3c8a4b32.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
下图为打印的结果
![](http://upload-images.jianshu.io/upload_images/1981026-d017e1131cfecf48.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
由于时间关系,暂时只整理了这些,后续再更新
此文也可以进入我的简书阅读
1、普通断点(经常用到)
当程序运行到断点处时会停止运行,只要在显示行数的相对应的地方点击一下就添加了一个端点。![](http://upload-images.jianshu.io/upload_images/1981026-4086d273ed977ead.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
添加的端点可以在如下图的地方查看。
![](http://upload-images.jianshu.io/upload_images/1981026-c87d6e59c452ffab.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
程序运行到断点处,可以看到如下图显示的参数
![](http://upload-images.jianshu.io/upload_images/1981026-70455e01ced699d9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2、条件断点(看自己情况而定使用)
在断点处右击按如下图选择![](http://upload-images.jianshu.io/upload_images/1981026-36cd952c80cf2d70.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在弹出框中设置你想中断程序的条件
![](http://upload-images.jianshu.io/upload_images/1981026-e9b949de7e051e73.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
下图就是程序中断之前打印出的结果
![](http://upload-images.jianshu.io/upload_images/1981026-cf300723efbc5b7a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
还可以通过设置Ignore的参数,忽略前n次的断点运行,程序会在第n+1 次的时候中断
![](http://upload-images.jianshu.io/upload_images/1981026-35c215ac8869f1b7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
下图是运行2次后中断结果
![](http://upload-images.jianshu.io/upload_images/1981026-30be122ea36bd2c5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
还可以通过添加 Action 来进行更多的配置,下图是查看断点所在处的方法调用的次数, %B 是断点所在的方法名, %H是断点调用的次数,注意:选中 Options
![](http://upload-images.jianshu.io/upload_images/1981026-90365e7cfad5235d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
下图为打印的结果
![](http://upload-images.jianshu.io/upload_images/1981026-ff7e0ecf4255f319.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
添加Action中的 Sound,会让程序有节奏的调用,不回马上执行完,在调用的同时可以听到方法调用的声音
![](http://upload-images.jianshu.io/upload_images/1981026-b7b55911e29771cf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3、异常断点(经常使用)
下图为添加异常断点的操作地方,首先按图中上面的尖头点击进入这个模式,再点击左下角 + 按钮添加![](http://upload-images.jianshu.io/upload_images/1981026-0acc22a62abee9d0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
下图为添加后的结果
![](http://upload-images.jianshu.io/upload_images/1981026-5dc1f13ffe7ba181.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
添加异常断点后,当程序遇到异常crash时,程序就会在出现异常代码的地方终止。如果没有打异常断点程序会在main.m中终止。下图为数组越界的一个示范
![](http://upload-images.jianshu.io/upload_images/1981026-b951cd9514a57962.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
4、符号断点
创建方式如下图![](http://upload-images.jianshu.io/upload_images/1981026-c5e70a2a1ded3e74.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
符号断点可以在你指定的[类名 方法名]中中断执行,如下图添加,此时程序会在ViewController类的loadData中中断
![](http://upload-images.jianshu.io/upload_images/1981026-e5ffb04cfe3f38fc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
还可以只添加方法,此时程序会在整个工程中只要遇到该方法就会中断
![](http://upload-images.jianshu.io/upload_images/1981026-49e8a850fe6cf590.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
下图为程序中断地方
![](http://upload-images.jianshu.io/upload_images/1981026-b81096f5be7742b5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
5、僵尸对象
在iOS开发中,把已经release但还没完全消失的对象称作僵尸对象,对已经release对的对象再次释放,就会发生异常。使用ARC后,对象释放产生的异常已经很少,但是偶尔还是会出现。开启僵尸对象模式后就能快速定位异常位置。开启的方式如下:Product-->Scheme-->Edit Scheme,再按照下图勾选就可以了。
![](http://upload-images.jianshu.io/upload_images/1981026-da6b3a03c0e458f7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
6、lldb命令
Xcode中使用的llvm编译器,而lldb是llvm中的调试器,我们可以使用一些简单的命令进行调试,拿上面那个for循环作为测试代码![](http://upload-images.jianshu.io/upload_images/1981026-182c360ba26301ce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
程序运行后中止在断点处,在下方打印出区域使用po命令、print命令打出相对应的信息,如下图
![](http://upload-images.jianshu.io/upload_images/1981026-569ef84afac24870.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
7、Log打印
NSLog打印信息一般是初学者最喜欢的打印,通过打印出的信息产看程序运行的结果,但是打印的信息较少,本身NSLog效率也比较低,使用下面的这个宏能够打印出所在类名、所在方法名、行号、具体时间。#import"ViewController.h"
#define DebugLog(fmt, ...) do { \
fprintf(stderr,"<%s> %s Line %d\n", [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String],\
__PRETTY_FUNCTION__, __LINE__); \
(NSLog)((fmt), ##__VA_ARGS__); \
fprintf(stderr,"----------\n"); \
} while (0)
@interfaceViewController()
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
[selfloadData];
}
- (void)loadData
{
intcount =10;
while(count <15) {
DebugLog(@"count = %d",count);
count ++;
}
}
![](http://upload-images.jianshu.io/upload_images/1981026-8d14efaa3c8a4b32.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
下图为打印的结果
![](http://upload-images.jianshu.io/upload_images/1981026-d017e1131cfecf48.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
由于时间关系,暂时只整理了这些,后续再更新
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- Linux 自检和 SystemTap
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- Python 七步捉虫法
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 路由器的配置与调试
- 对于技术人员的出现了运行时间错误,是否要进行调试的解决方法
- 在ASP.NET 2.0中操作数据之七十二:调试存储过程
- 讲解WordPress开发中一些常用的debug技巧
- JavaScript程序设计之JS调试
- 可以用来调试JavaScript错误的解决方案
- 如何调试异步加载页面里包含的js文件
- jQuery下的Ajax调试步骤
- 调试一段PHP程序时遇到的三个问题
- JavaScript高级程序设计 错误处理与调试学习笔记
- Javascript调试脚本的经验之谈第1/2页