NSAssert的使用
https://www.jianshu.com/p/526dfd5cbb19
2016.08.04 12:06*?字?jǐn)?shù) 517?閱讀 3504評(píng)論 3喜歡 3NSAssert是一個(gè)預(yù)處理宏, 他的主要作用就是可以讓開(kāi)發(fā)者比較便捷的捕獲一個(gè)錯(cuò)誤, 讓程序崩潰, 同時(shí)報(bào)出錯(cuò)誤提示
使用方法:
- NSAssert(x != nil, @"錯(cuò)誤提示")
- 當(dāng)你的程序在運(yùn)行到這個(gè)宏時(shí), 如果變量x的值為nil, 此時(shí)程序就會(huì)崩潰, 并且拋出一個(gè)異常, 異常提示就是你后面寫(xiě)的提示
一些注意點(diǎn)
- 錯(cuò)誤提示是可以使用中文的
- NSAssert還有幾個(gè)可以接收參數(shù)的宏
- 如:?NSAssert2(condition, desc, arg1, arg2)
- 他的作用是可以讓你的desc描述語(yǔ)句, 添加上參數(shù), 讓你的異常捕獲更加精確
發(fā)布版本
- NSAssert也是一個(gè)預(yù)處理指令, 如果使用過(guò)多, 也會(huì)影響你的程序運(yùn)行, 這時(shí)我們要像在發(fā)布版本時(shí)處理NSLog一樣處理這個(gè)預(yù)處理指令, 只不過(guò)他的處理方式有些不同
- 首先進(jìn)入項(xiàng)目工程文件
- 選擇Build Settings
- 搜索Perprocessor Macros
- 在Release中添加一個(gè)規(guī)則:?NS_BLOCK_ASSERTIONS
- 這時(shí)當(dāng)你的APP處于發(fā)布版本時(shí), 這個(gè)預(yù)處理指令就會(huì)失效了
使用效果如下
2016-08-04 11:58:55.225 NSAssert[4215:68280] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '提示10, 20'
?*** First throw call stack:
?(
?? ? 0 ? CoreFoundation? ? ? ? ? ? ? ? ? ? ? 0x000000010c0fce65 __exceptionPreprocess + 165
?? ? 1 ? libobjc.A.dylib ? ? ? ? ? ? ? ? ? ? 0x000000010bb75deb objc_exception_throw + 48
?? ? 2 ? CoreFoundation? ? ? ? ? ? ? ? ? ? ? 0x000000010c0fccca +[NSException raise:format:arguments:] + 106
?? ? 3 ? Foundation? ? ? ? ? ? ? ? ? ? ? ? ? 0x000000010b7c24de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
?? ? 4 ? NSAssert? ? ? ? ? ? ? ? ? ? ? ? ? ? 0x000000010b6777f0 -[ViewController viewDidLoad] + 336
?? ? 5 ? UIKit ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0x000000010c63ff98 -[UIViewController loadViewIfRequired] + 1198
?? ? 6 ? UIKit ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0x000000010c6402e7 -[UIViewController view] + 27
?? ? 7 ? UIKit ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0x000000010c516ab0 -[UIWindow addRootViewControllerViewIfPossible] + 61
?? ? 8 ? UIKit ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0x000000010c517199 -[UIWindow _setHidden:forced:] + 282
?? ? 9 ? UIKit ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0x0000000119d4c831 -[UIWindowAccessibility _orderFrontWithoutMakingKey] + 68
?? ? 10? UIKit ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0x000000010c528c2e -[UIWindow makeKeyAndVisible] + 42
?? ? 11? UIKit ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0x000000010c4a1663 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4131
?? ? 12? UIKit ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0x000000010c4a7cc6 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1760
?? ? 13? UIKit ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0x000000010c4a4e7b -[UIApplication workspaceDidEndTransaction:] + 188
?? ? 14? FrontBoardServices? ? ? ? ? ? ? ? ? 0x000000010ee75754 -[FBSSerialQueue _performNext] + 192
?? ? 15? FrontBoardServices? ? ? ? ? ? ? ? ? 0x000000010ee75ac2 -[FBSSerialQueue _performNextFromRunLoopSource] + 45
?? ? 16? CoreFoundation? ? ? ? ? ? ? ? ? ? ? 0x000000010c028a31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
?? ? 17? CoreFoundation? ? ? ? ? ? ? ? ? ? ? 0x000000010c01e95c __CFRunLoopDoSources0 + 556
?? ? 18? CoreFoundation? ? ? ? ? ? ? ? ? ? ? 0x000000010c01de13 __CFRunLoopRun + 867
?? ? 19? CoreFoundation? ? ? ? ? ? ? ? ? ? ? 0x000000010c01d828 CFRunLoopRunSpecific + 488
?? ? 20? UIKit ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0x000000010c4a47cd -[UIApplication _run] + 402
?? ? 21? UIKit ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0x000000010c4a9610 UIApplicationMain + 171
?? ? 22? NSAssert? ? ? ? ? ? ? ? ? ? ? ? ? ? 0x000000010b677b3f main + 111
?? ? 23? libdyld.dylib ? ? ? ? ? ? ? ? ? ? ? 0x000000010e83892d start + 1
?? ? 24? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0x0000000000000001 0x0 + 1
?)
?libc++abi.dylib: terminating with uncaught exception of type NSException
轉(zhuǎn)載于:https://www.cnblogs.com/sundaysgarden/p/10694321.html
總結(jié)
以上是生活随笔為你收集整理的NSAssert的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 未来耳机可能将成为最强大的健康监护仪
- 下一篇: React + TypeScript:元