您的位置:首页 > 移动开发 > IOS开发

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

2016-03-27 08:59 489 查看
经常可以看到第三方类库的代码会用到断言, 在此总结一下断言的问题

一、Objective - C 中的断言:
Objective - C 中的断言处理使用的是 NSAssertionHandler :

1.每个线程拥有它自己的断言处理器, 它是NSAssertionHandler类的实例对象, 当被调用时, 一个断言处理器打印一条包含方法和类名(函数名)的错误信息.然后它抛出一个NSInternallnconsistencyException异常.

2.基础类中定义了两套断言宏 NSAssert / NSCAssert
/** NSAssert */
#if !defined(_NSAssertBody)
#define NSAssert(condition, desc, ...)    \
<pre name="code" class="objc">do {				\
__PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \
if (!(condition)) {		\
NSString *__assert_file__ = [NSString stringWithUTF8String:__FILE__]; \
__assert_file__ = __assert_file__ ? __assert_file__ : @"<Unknown File>"; \
[[NSAssertionHandler currentHandler] handleFailureInMethod:_cmd \
object:self file:__assert_file__ \
lineNumber:__LINE__ description:(desc), ##__VA_ARGS__]; \
}				\
__PRAGMA_POP_NO_EXTRA_ARG_WARNINGS \
} while(0)

/** NSCAssert */
#if !defined(_NSCAssertBody)
#define NSCAssert(condition, desc, ...) \
do {                \

    __PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \

    if (!(condition)) {        \

            NSString *__assert_fn__ = [NSString stringWithUTF8String:__PRETTY_FUNCTION__]; \

            __assert_fn__ = __assert_fn__ ? __assert_fn__ : @"<Unknown Function>"; \

            NSString *__assert_file__ = [NSString stringWithUTF8String:__FILE__]; \

            __assert_file__ = __assert_file__ ? __assert_file__ : @"<Unknown File>"; \

        [[NSAssertionHandler currentHandler] handleFailureInFunction:__assert_fn__ \

        file:__assert_file__ \

            lineNumber:__LINE__ description:(desc), ##__VA_ARGS__]; \

    }                \

        __PRAGMA_POP_NO_EXTRA_ARG_WARNINGS \

    } while(0)


除此之外还有  NSParameterAssert / NSCParameterAssert 详情百度吧, 这里就不多做解释了

二.断言的使用(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 benon-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 appdue 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 +[NSExceptionraise:format:arguments:] + 104

3   Foundation                     0x00007fff88dae6a2 -[NSAssertionHandlerhandleFailureInMethod:object:file:lineNumber:description:] +189

4   lottery                        0x0000000100001929 -[LotteryEntryinitWithEntryDate:] + 249

5   lottery                        0x0000000100001794 main + 932

6   libdyld.dylib                   0x00007fff8d83f7e1 start + 0

)

libc++abi.dylib: terminate called throwing an exception
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: