ios之断言更胜于NSLog

有时候我们需要不断的输出以用来调试程序,断言这个东西很少被人用到。今天看网上一大神写的代码

NSAssert(!gMenu, @"singleton object");

NSParameterAssert(view);

便查了下文档

#define NSParameterAssert(condition) NSAssert((condition), @"Invalid parameter not satisfying: %s", #condition)

#if !defined(_NSAssertBody)
#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)
#endif

本质还是NSAssert。

第一个参数为一个条件判断,如果为假,则抛出异常,显示第二个参数所描述的信息。

举个例子:

    NSInteger a = 5;
    NSInteger b = 0;
    NSInteger c = 0;
    // 断言
    NSAssert(b!=0, @"b 是 零");

只有在debug下,会导致crash,并在系统中显示:

2014-07-23 13:43:33.465 CatchCrash[8747:60b] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException‘, reason: ‘b 是 零‘
*** First throw call stack:
(
	0   CoreFoundation                      0x0000000101947495 __exceptionPreprocess + 165
	1   libobjc.A.dylib                     0x00000001016a699e objc_exception_throw + 43
	2   CoreFoundation                      0x000000010194731a +[NSException raise:format:arguments:] + 106
	3   Foundation                          0x0000000101242f19 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 189
	4   CatchCrash                          0x0000000100001850 -[ViewController viewDidLoad] + 1056
	5   UIKit                               0x000000010034959e -[UIViewController loadViewIfRequired] + 562
	6   UIKit                               0x0000000100349777 -[UIViewController view] + 29
	7   UIKit                               0x000000010028996b -[UIWindow addRootViewControllerViewIfPossible] + 58
	8   UIKit                               0x0000000100289c70 -[UIWindow _setHidden:forced:] + 282
	9   UIKit                               0x0000000111039d73 -[UIWindowAccessibility(SafeCategory) _orderFrontWithoutMakingKey] + 68
	10  UIKit                               0x0000000100292ffa -[UIWindow makeKeyAndVisible] + 51
	11  UIKit                               0x000000010024ec98 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1788
	12  UIKit                               0x0000000100252a0c -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 660
	13  UIKit                               0x0000000100263d4c -[UIApplication handleEvent:withNewEvent:] + 3189
	14  UIKit                               0x0000000100264216 -[UIApplication sendEvent:] + 79
	15  UIKit                               0x0000000100254086 _UIApplicationHandleEvent + 578
	16  GraphicsServices                    0x0000000103ac071a _PurpleEventCallback + 762
	17  GraphicsServices                    0x0000000103ac01e1 PurpleEventCallback + 35
	18  CoreFoundation                      0x00000001018c9679 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
	19  CoreFoundation                      0x00000001018c944e __CFRunLoopDoSource1 + 478
	20  CoreFoundation                      0x00000001018f2903 __CFRunLoopRun + 1939
	21  CoreFoundation                      0x00000001018f1d83 CFRunLoopRunSpecific + 467
	22  UIKit                               0x00000001002522e1 -[UIApplication _run] + 609
	23  UIKit                               0x0000000100253e33 UIApplicationMain + 1010
	24  CatchCrash                          0x0000000100001c47 main + 103
	25  libdyld.dylib                       0x0000000101fdf5fd start + 1
	26  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)


上面代码区第一行少了一些字符串,在下面重新写了下。就是写出了crash的原因

*** Terminating app due to uncaught exception ‘NSInternalInconsistencyException‘, reason: ‘b

*** First throw call stack:



ios之断言更胜于NSLog,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。