assert断言,assert(表达式),表达式为真,什么也不做,表达式为假则crash。
assert在OC里面是一个宏,用法一样,只有在Debug状态下有作用。
NSAssert和assert一样也是一个宏,用法也差不多,只是多了在让程序crash时可以报自定义的log异常,这个也是只在Debug状态下有用
在app中加入一行代码
NSAssert(0, @"哈哈哈哈");
Debug状态下app崩溃
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '哈哈哈哈'
*** First throw call stack:
(
0 CoreFoundation 0x00000001096d8d85 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010914cdeb objc_exception_throw + 48
2 CoreFoundation 0x00000001096d8bea +[NSException raise:format:arguments:] + 106
3 Foundation 0x0000000108d96d5a -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
4 ZhuoYu 0x0000000108c21ea8 -[HomeViewController viewDidLoad] + 520
5 UIKit 0x0000000109c29984 -[UIViewController loadViewIfRequired] + 1198
6 UIKit 0x0000000109c6dfae -[UINavigationController _layoutViewController:] + 54
7 UIKit 0x0000000109c6e882 -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 462
8 UIKit 0x0000000109c6e9f4 -[UINavigationController _startTransition:fromViewController:toViewController:] + 126
9 UIKit 0x0000000109c6fc4d -[UINavigationController _startDeferredTransitionIfNeeded:] + 890
10 UIKit 0x0000000109c70d0b -[UINavigationController __viewWillLayoutSubviews] + 57
11 UIKit 0x0000000109e1f503 -[UILayoutContainerView layoutSubviews] + 248
12 UIKit 0x0000000109b49980 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
13 QuartzCore 0x000000010db70c00 -[CALayer layoutSublayers] + 146
14 QuartzCore 0x000000010db6508e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
15 QuartzCore 0x000000010db64f0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
16 QuartzCore 0x000000010db593c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
17 QuartzCore 0x000000010db87086 _ZN2CA11Transaction6commitEv + 486
18 QuartzCore 0x000000010db877f8 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
19 CoreFoundation 0x00000001095fdc37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
20 CoreFoundation 0x00000001095fdba7 __CFRunLoopDoObservers + 391
21 CoreFoundation 0x00000001095f311c CFRunLoopRunSpecific + 524
22 UIKit 0x0000000109a89f21 -[UIApplication _run] + 402
23 UIKit 0x0000000109a8ef09 UIApplicationMain + 171
24 ZhuoYu 0x0000000108c21c7f main + 111
25 libdyld.dylib 0x000000010beb292d start + 1
)
release状态下,运行正常。
网友评论