NSAssert简单使用

在不断填坑中前进。。

Posted by 三十一 on August 28, 2017

NSAssert简单使用

NSAssert 的定义如下:

#define NSAssert(condition, desc, ...)	\
    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)
#endif
  • 当 condition 判断条件为 假 的时候,程序终止,提示错误,提示语为 desc。
  • 当 condition 判断条件为 真 的时候,程序继续运行。
 NSAssert(self.umPushAppKey != nil, @"请设置友盟key");
  • 当 self.umPushAppKey 为 nil,判断条件 self.umPushAppKey != nil 为 假,程序终止,提示 请设置友盟key。
    • 提示语为: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '请设置友盟key'
  • 当 self.umPushAppKey 不为 nil,判断条件 self.umPushAppKey != nil 为 真,程序继续运行。
 NSAssert(self.umPushAppKey, @"请设置友盟key");
  • 当 self.umPushAppKey 为 nil,判断条件 self.umPushAppKey 为 假,程序终止,提示 请设置友盟key。
    • 提示语为: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '请设置友盟key'
  • 当 self.umPushAppKey 不为 nil,判断条件 self.umPushAppKey 为 真,程序继续运行。