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.umPushAppKeynil,判断条件 self.umPushAppKey != nil,程序终止,提示 请设置友盟key
    • 提示语为: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '请设置友盟key'
  • self.umPushAppKey 不为 nil,判断条件 self.umPushAppKey != nil,程序继续运行。
 NSAssert(self.umPushAppKey, @"请设置友盟key");
  • self.umPushAppKeynil,判断条件 self.umPushAppKey,程序终止,提示 请设置友盟key
    • 提示语为: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '请设置友盟key'
  • self.umPushAppKey 不为 nil,判断条件 self.umPushAppKey,程序继续运行。