NSAssert()
来源:互联网 发布:linux vnc服务器 编辑:程序博客网 时间:2024/06/09 14:51
使用断言NSAssert()调试程序错误
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 + 198
1 libobjc.A.dylib 0x00007fff8fd2a3f0 objc_exception_throw + 43
2 CoreFoundation 0x00007fff90c58ee8 +[NSException raise:format:arguments:] + 104
3 Foundation 0x00007fff88dae6a2 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 189
4 lottery 0x0000000100001929 -[LotteryEntry initWithEntryDate:] + 249
5 lottery 0x0000000100001794 main + 932
6 libdyld.dylib 0x00007fff8d83f7e1 start + 0
)
libc++abi.dylib: terminate called throwing an exception
转:http://blog.sina.com.cn/s/blog_75992b660101kbj2.html
- NSAssert
- NSAssert()
- NSAssert
- NSAssert()
- NSAssert()
- NSAssert
- NSAssert用法
- iPhone-NSAssert
- NSAssert,NSCassert
- iOS NSAssert
- 使用NSAssert
- NSAssert,NSCassert
- NSAssert使用
- 小心NSAssert
- NSAssert()断言
- NSAssert用法
- NSAssert用法
- NSAssert基础
- GCD高级功能(一次性执行,调度组,延迟操作)
- windows下vs2010 编译live555源码库的环境搭建
- CSS文档规范
- 苹果原生框架分享 ----- Social
- android RSA加密的使用
- NSAssert()
- 实现单张图片的倒影效果
- 343. Integer Break
- FileReader搭配FileWriter,更新对抛出的机制做回应
- poj2594
- 基于JavaScript代码去掉H5页面中的头尾及广告部分(支持 Android 和 iOS)
- Unity3D的几种坐标系,以及屏幕解锁类似功能
- hdu1978——How many ways(记忆化dp)
- apache commons fileupload 问题跟踪系统