断言(Assert):
在程序设计中,断言(assertion)是一种放在程序中的一阶逻辑(如一个结果为真或是假的逻辑判断式),目的是为了标示与验证程序开发者预期的结果-当程序运行到断言的位置时,对应的断言应该为真。若断言不为真时,程序会中止运行,并给出错误消息。(《维基百科》)
iOS中,NSAssert 是一个宏,其定义为:
#define NSAssert(condition, desc, ...) \
do { \
__PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \
if (!(condition)) { \
[[NSAssertionHandler currentHandler] handleFailureInMethod:_cmd \
object:self file:[NSString stringWithUTF8String:__FILE__] \
lineNumber:__LINE__ description:(desc), ##__VA_ARGS__]; \
} \
__PRAGMA_POP_NO_EXTRA_ARG_WARNINGS \
} while(0)
NSAssert可以给代码带来很大的方便,其使用方法很简单。第一个参数condition为一个返回YES或NO的表达式;第二个参数desc为前一个表达式验证不通过时输出的信息。
例:我们在程序中添加如下两行代码:
int i = 1;
NSAssert(i < 0, @"i 应该是负数");
执行时,程序会报错中断,控制台输出如下:
2015-09-01 10:32:27.120 TestiOS[1091:49373] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'i 应该是负数'
*** First throw call stack:
(
0 CoreFoundation 0x00000001043bcc65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000104055bb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001043bcaca +[NSException raise:format:arguments:] + 106
3 Foundation 0x0000000103c6a98f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4 TestiOS 0x0000000103b248b4 -[ViewController viewDidLoad] + 228
5 UIKit 0x00000001048e61d0 -[UIViewController loadViewIfRequired] + 738
6 UIKit 0x00000001048e63ce -[UIViewController view] + 27
7 UIKit 0x0000000104801289 -[UIWindow addRootViewControllerViewIfPossible] + 58
8 UIKit 0x000000010480164f -[UIWindow _setHidden:forced:] + 247
9 UIKit 0x000000010480dde1 -[UIWindow makeKeyAndVisible] + 42
10 UIKit 0x00000001047b1417 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2732
11 UIKit 0x00000001047b419e -[UIApplication _runWithMainScene:transitionContext:completion:] + 1349
12 UIKit 0x00000001047b3095 -[UIApplication workspaceDidEndTransaction:] + 179
13 FrontBoardServices 0x0000000106f765e5 __31-[FBSSerialQueue performAsync:]_block_invoke_2 + 21
14 CoreFoundation 0x00000001042f041c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
15 CoreFoundation 0x00000001042e6165 __CFRunLoopDoBlocks + 341
16 CoreFoundation 0x00000001042e5947 __CFRunLoopRun + 887
17 CoreFoundation 0x00000001042e5366 CFRunLoopRunSpecific + 470
18 UIKit 0x00000001047b2b02 -[UIApplication _run] + 413
19 UIKit 0x00000001047b58c0 UIApplicationMain + 1282
20 TestiOS 0x0000000103b24d2f main + 111
21 libdyld.dylib 0x0000000106960145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
通常在开发过程,debug版会执行所有断言检查,release版不会进行断言检查。这样我们通过更改项目开发模式便可以很便捷的调试程序。设置开发版本是debug还是release的地方在:Product -> Scheme -> Edit Scheme