標籤:des style os strong io 2014
有時候我們需要不斷的輸出以用來偵錯工具,斷言這個東西很少被人用到。今天看網上一大神寫的代碼
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 + 1651 libobjc.A.dylib 0x00000001016a699e objc_exception_throw + 432 CoreFoundation 0x000000010194731a +[NSException raise:format:arguments:] + 1063 Foundation 0x0000000101242f19 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 1894 CatchCrash 0x0000000100001850 -[ViewController viewDidLoad] + 10565 UIKit 0x000000010034959e -[UIViewController loadViewIfRequired] + 5626 UIKit 0x0000000100349777 -[UIViewController view] + 297 UIKit 0x000000010028996b -[UIWindow addRootViewControllerViewIfPossible] + 588 UIKit 0x0000000100289c70 -[UIWindow _setHidden:forced:] + 2829 UIKit 0x0000000111039d73 -[UIWindowAccessibility(SafeCategory) _orderFrontWithoutMakingKey] + 6810 UIKit 0x0000000100292ffa -[UIWindow makeKeyAndVisible] + 5111 UIKit 0x000000010024ec98 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 178812 UIKit 0x0000000100252a0c -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 66013 UIKit 0x0000000100263d4c -[UIApplication handleEvent:withNewEvent:] + 318914 UIKit 0x0000000100264216 -[UIApplication sendEvent:] + 7915 UIKit 0x0000000100254086 _UIApplicationHandleEvent + 57816 GraphicsServices 0x0000000103ac071a _PurpleEventCallback + 76217 GraphicsServices 0x0000000103ac01e1 PurpleEventCallback + 3518 CoreFoundation 0x00000001018c9679 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 4119 CoreFoundation 0x00000001018c944e __CFRunLoopDoSource1 + 47820 CoreFoundation 0x00000001018f2903 __CFRunLoopRun + 193921 CoreFoundation 0x00000001018f1d83 CFRunLoopRunSpecific + 46722 UIKit 0x00000001002522e1 -[UIApplication _run] + 60923 UIKit 0x0000000100253e33 UIApplicationMain + 101024 CatchCrash 0x0000000100001c47 main + 10325 libdyld.dylib 0x0000000101fdf5fd start + 126 ??? 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: