使用断言NSAssert()调试程序错误
来源:互联网 发布:官路淘宝全文免费阅读 编辑:程序博客网 时间:2024/05/18 00:24
NSAssert()只是一个宏,用于开发阶段调试程序中的Bug,通过为NSAssert()传递条件表达式来断定是否属于Bug,满足条件返回真值,程序继续运行,如果返回假值,则抛出异常,并切可以自定义异常描述。NSAssert()是这样定义的:
#define NSAssert(condition, desc)
condition是条件表达式,值为YES或NO;desc为异常描述,通常为NSString。当conditon为YES时程序继续运行,为NO时,则抛出带有desc描述的异常信息。NSAssert()可以出现在程序的任何一个位置。具体事例如下:
生成一个LotteryEntry对象时,传入的NSDate不能为nil,加入NSAssert()判断。对象初始化源码如下:
- (id)initWithEntryDate:(NSDate *)theDate { self = [super init]; if (self) { NSAssert(theDate != nil, @"Argument must be non-nil"); entryDate = theDate; firstNumber = (int)random() % 100 + 1; secondNumber = (int)random() % 100 + 1; } return self;}
接下来则是生成对象时传入一个值为nil的NSDate,看断言是否运行。
LotteryEntry *nilEntry = [[LotteryEntry alloc] initWithEntryDate:nil];
断言效果如下:
2013-01-17 20:49:12.486 lottery[3951:303] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Argument must be non-nil'*** First throw call stack:(0 CoreFoundation 0x00007fff90c590a6 __exceptionPreprocess + 1981 libobjc.A.dylib 0x00007fff8fd2a3f0 objc_exception_throw + 432 CoreFoundation 0x00007fff90c58ee8 +[NSException raise:format:arguments:] + 1043 Foundation 0x00007fff88dae6a2 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 1894 lottery 0x0000000100001929 -[LotteryEntry initWithEntryDate:] + 2495 lottery 0x0000000100001794 main + 9326 libdyld.dylib 0x00007fff8d83f7e1 start + 0)libc++abi.dylib: terminate called throwing an exception
原文地址
0 0
- iOS 使用断言NSAssert()调试程序错误
- 使用断言NSAssert()调试程序错误
- 使用断言NSAssert()调试程序错误
- 使用断言NSAssert()调试程序错误
- iOS 使用断言NSAssert()调试程序错误
- 使用断言NSAssert()和NSParameterAssert调试程序错误
- [IOS 开发] 使用断言NSAssert()调试程序错误
- 使用断言NSAssert()和NSParameterAssert调试程序错误
- iOS 断言 NSAssert的使用 调试程序错误
- 使用断言NSAssert()和NSParameterAssert调试程序错误
- 断言NSAssert()-调试程序中的Bug
- NSAssert断言的使用
- 使用NSAssert()和NSParameterAssert调试程序
- iOS --- 使用NSAssert来调试程序
- iOS断言 NSAssert的使用
- iOS 断言(NSAssert)的使用
- ios NSAssert调试程序
- NSAssert调试程序
- MQTT学习笔记-MQTT简要介绍
- mysql删除表的一列或者修改一列的名称和属性
- 040.category 调用私有方法
- 新泽西博彩监管机构发布扑克之星报告
- 编译工具设置
- 使用断言NSAssert()调试程序错误
- ubuntu: apt-get update的时候遇到“E: Some index files failed to download”错误
- 039.category 对类进行模块化设计
- Android开发环境搭建
- Linux常见问题
- eisenhower1拿到9月月度PLB冠军
- 038.category 分类/类别
- 【codevs1959】拔河比赛 随机化贪心
- Kafka学习1_kafka入门:简介、使用场景、设计原理、主要配置及集群搭建