NSAssert()

开发ios程序时调试的好帮手---NSAssert()函数。而且和NSLog()函数一样简单易用

代码如下:    NSAssert(x!=0,@"x must not be zero");


在表达式“x!=0”不成立时,程序就会抛出异常,并显示自定义的消息"x must not be zero",并同时显示出错的文件、代码和调用函数等信息,是一个程序追踪的很好手段。

假设x!=0,不满足要求就提示reason-x must not be zero


再如以下应用:

NSAssert(dispatch_get_current_queue() ==storageQueue, @"Invoked on incorrect queue");


assert 是C里面的宏。用于断言。


NSAssert 只能在Objective-c里面使用。是assert的一个扩充。能捕获assert类异常及打印一些

可读的日志。而assert只是让app crash(abort).

参考 : nsassert-vs-assert-which-do-you-use-and-when

The basic difference between an NSAssert and a regular assert is that an NSAssertraises an exception when it fails while an assert just crashes the app. NSAssert also lets you supply fancier error messages and logs them. Practically, I really don't think there's much difference between the two--I can't think of a reason to handle an exception thrown by an assertion. (To split hairs, I think NSAssert usually involves less typing because you don't have to includeassert.h, but that's neither here nor there.)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值