iOS 使用断言NSAssert()调试程序错误

本文链接:http://blog.sina.com.cn/s/blog_75992b660101kbj2.html

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 allocinitWithEntryDate: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

程序一般分为Debug 版本和Release 版本,Debug 版本用于内部调试,Release 版本发行给用户使用。(这个概念可能不大好理解,想想VC下,调试时有个选项,一个是debug,一个是release)

assert(表达式); 的意思是:当表达式为真时,程序继续运行,如果表达市为假,那程序就会停止运行,并提示错误信息。

注意:assert是一个宏,只在debug版本中起作用,在release版本中,该语句是不起任何作用的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值