美文网首页IOS基础知识
一. NSAssert的使用

一. NSAssert的使用

作者: 面糊 | 来源:发表于2016-08-04 12:06 被阅读1360次
  1. NSAssert是一个预处理宏, 他的主要作用就是可以让开发者比较便捷的捕获一个错误, 让程序崩溃, 同时报出错误提示

  2. 使用方法:

    • NSAssert(x != nil, @"错误提示")
    • 当你的程序在运行到这个宏时, 如果变量x的值为nil, 此时程序就会崩溃, 并且抛出一个异常, 异常提示就是你后面写的提示
  3. 一些注意点

    • 错误提示是可以使用中文的
    • NSAssert还有几个可以接收参数的宏
    • 如: NSAssert2(condition, desc, arg1, arg2)
    • 他的作用是可以让你的desc描述语句, 添加上参数, 让你的异常捕获更加精确
  4. 发布版本

    • NSAssert也是一个预处理指令, 如果使用过多, 也会影响你的程序运行, 这时我们要像在发布版本时处理NSLog一样处理这个预处理指令, 只不过他的处理方式有些不同
      1. 首先进入项目工程文件
      2. 选择Build Settings
      3. 搜索Perprocessor Macros
      4. 在Release中添加一个规则: NS_BLOCK_ASSERTIONS
      5. 这时当你的APP处于发布版本时, 这个预处理指令就会失效了
  5. 使用效果如下

     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

相关文章

  • HQ移动20170317期周报

    1. NSAssert() 和 NSCAssert()的使用 NSAssert()用于 OC 语法的断言NSCAs...

  • 断言NSAssert的使用

    1. NSAssert 断言(NSAssert)是一个宏,在开发过程中使用NSAssert可以及时发现程序中的问题...

  • 一. NSAssert的使用

    NSAssert是一个预处理宏, 他的主要作用就是可以让开发者比较便捷的捕获一个错误, 让程序崩溃, 同时报出错误...

  • NSAssert 断言

    NSAssert 是c语言的一个宏定义。 看一下具体定义: 具体使用: NSAssert(condition, ...

  • 断言

    在iOS开发中,可以使用宏NSAssert()在程序中进行断言处理。NSAssert()使用正确,可以帮助开发者尽...

  • IOS开发 日常踩坑记录

    Block 内部使用NSAssert导致的循环引用的问题

  • NSAssert()使用目的

    转自iOS开发中断言的使用—NSAssert()[https://blog.csdn.net/univcore/a...

  • NSAssert断言的使用

    NSAssert()只是一个宏,用于开发阶段调试程序中的Bug,通过为NSAssert()传递条件表达式来断定是否...

  • 断言(NSAssert)的使用

    NSAssert()是一个宏,用于开发阶段调试程序中的Bug,通过为NSAssert()传递条件表达式来断定是否属...

  • NSAssert 的简单使用

    苹果在foundation.framework中定义了这么一个宏: condition 书面意思为:状态、状况在这...

网友评论

本文标题:一. NSAssert的使用

本文链接:https://www.haomeiwen.com/subject/jzpcsttx.html