2016.05.05 18:34* 字数 861 阅读 5127评论 0喜欢 17
https://www.jianshu.com/p/3072e174554f

NSAssert和NSParameterAssert在开发环境中经常被使用,调试和验证代码参数的完整性,断言为真,则表明程序运行正常,而断言为假,则意味着它已经在代码中发现了意料之外的错误。xCode中的断言在Debug模式默认是开启的,Realse版本中是禁用的.

基础断言

基础类库中了两种断言,NSAssert和NSParameterAssert是OC断言,NSCAssert和NSCParameterAssert是C语言断言。先来看一下NSAssert定义:
<pre><code>The NSAssert macro evaluates the condition and serves as a front end to the assertion handler. Each thread has its own assertion handler, which is an object of class NSAssertionHandler. When invoked, an assertion handler prints an error message that includes the method and class names (or the function name). It then raises an NSInternalInconsistencyException exception. If condition evaluates to NO, the macro invokes handleFailureInMethod:object:file:lineNumber:description: on the assertion handler for the current thread, passing desc as the description string. This macro should be used only within Objective-C methods. Assertions are disabled if the preprocessor macro NS_BLOCK_ASSERTIONS is defined. **Important:Important** Do not call functions with side effects in the condition parameter of this macro. The condition parameter is not evaluated when assertions are disabled, so if you call functions with side effects, those functions may never get called when you build the project in a non-debug configuration. **Note:Note** Not all release configurations disable assertions by default.</code></pre>
NSParameterAssert的定义:
<pre>Assertions evaluate a condition and, if the condition evaluates to false, call the assertion handler for the current thread, passing it a format string and a variable number of arguments. Each thread has its own assertion handler, which is an object of class NSAssertionHandler. When invoked, an assertion handler prints an error message that includes method and class names (or the function name). It then raises an NSInternalInconsistencyException exception. This macro validates a parameter for an Objective-C method. Simply provide the parameter as the condition argument. The macro evaluates the parameter and, if it is false, it logs an error message that includes the parameter and then raises an exception. Assertions are disabled if the preprocessor macro NS_BLOCK_ASSERTIONS is defined. All assertion macros return void. **Important:Important** Do not call functions with side effects in the condition parameter of this macro. The condition parameter is not evaluated when assertions are disabled, so if you call functions with side effects, those functions may never get called when you build the project in a non-debug configuration. **Note:Note** Not all release configurations disable assertions by default.</pre>
两者的定义类似,大概意思就是如果是false就会调用当前线程Assertion Hanlder进行处理,非Debug模式下可能所有的断言都不会调用,最后一句很重要,并不是所有的发布配置会禁用断言,如果想看断言是否禁用,需要看一下设置:

Snip20160505_1.png

简单测试:
<pre><code>`
NSString *result=@"中山郎";
NSInteger count=10;
NSAssert(count>10, @"总数必须大于10");
NSLog(@"断言执行之后");

</code></pre> 崩溃信息: <pre><code>
** FECategory[23811:248235] *** Assertion failure in -[ViewController setupAssert], /ViewController.m:45**
** FECategory[23811:248235] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '****总数必须大于****10'**`</code></pre>

NSAssertionHandler

NSAssert异常处理的时候默认是NSAssertionHandler处理的,不过我们可以自定自己的Handler,实现两个方法:
<pre><code>`

  • (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(nullable NSString *)format,... NS_FORMAT_FUNCTION(5,6);
  • (void)handleFailureInFunction:(NSString *)functionName file:(NSString *)fileName lineNumber:(NSInteger)line description:(nullable NSString *)format,... NS_FORMAT_FUNCTION(4,5);`</code></pre>

handleFailureInMethod处理OC方法中的断言,handleFailureInFunction处理C函数中的断言
自定义继承自NSAssertionHandler的类FEAssertionHandler:
<pre><code>`
@implementation FEAssertionHandler

-(void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format, ...{
NSLog(@"FlyElephant-FEAssertionHandler: Method %@ for object %@ in %@--line:%li", NSStringFromSelector(selector), object, fileName, (long)line);
}

-(void)handleFailureInFunction:(NSString *)functionName file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format, ...{
NSLog(@"FlyElephant-FEAssertionHandler:Function (%@) in %@--line:%li", functionName, fileName, (long)line);
}

@end
</code></pre> AppDelegate中设置断言处理: <pre><code>

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    FEAssertionHandler *hanlder=[[FEAssertionHandler alloc]init];
    [[[NSThread currentThread] threadDictionary] setValue:hanlder forKey:NSAssertionHandlerKey];
    return YES;
    }

转载于:https://www.cnblogs.com/sundaysgarden/p/10354170.html

NSAssert和NSParameterAssert相关推荐

  1. iOS-----程序异常处理----- 断言NSAssert()和NSParameterAssert区别和用处

    iOS-----程序异常处理----- 断言NSAssert()和NSParameterAssert区别和用处 参考文章: (1)iOS-----程序异常处理----- 断言NSAssert()和NS ...

  2. 在objective-c / cocoa中抛出异常

    在objective-c / cocoa中抛出异常的最佳方法是什么? #1楼 使用NSError来传达故障而不是异常. 关于NSError的快速点: NSError允许C样式错误代码(整数)清楚地标识 ...

  3. 什么是NSAssert?

    断言, 判断是否符合某个特定条件, 符合就继续运行程序, 反之就抛出异常, 后面为自定义错误提示, 也可以使用NSParameterAssert, 在调试上有着很大的方便 int a = 0; NSA ...

  4. NSAssert和NSLog

    开发ios程序时调试的好帮手---NSAssert()函数.而且和NSLog()函数一样简单易用,代码如下:    NSAssert(x!=0,@"x must not be zero&qu ...

  5. NSAssert的使用

    https://www.jianshu.com/p/526dfd5cbb19 2016.08.04 12:06* 字数 517 阅读 3504评论 3喜欢 3 NSAssert是一个预处理宏, 他的主 ...

  6. 断言NSAssert的使用

    NSAssert()的定义是 #define NSAssert(condition, desc) condition是条件表达式,值为yes或者no,在OC中规定0为假,非0就为真. desc为异常描 ...

  7. NSAssert与assert断言

    调试cocoa程序在程序出错时,不会马上停止.使用宏NSAssert可以让程序出错时马上抛出异常. 在debug情况下,所有NSAssert都会被执行.在release下不希望NSAssert被执行, ...

  8. iOS之深入解析如何使用Block实现委托方法

    一.前言 Block 和 Delegate 是对象间传递消息的常用机制,这两个机制可以说是各有千秋. Delegate 可以很方便把目标动作的执行过程划分为多个方法,以展现不同时间节点下特定的操作:B ...

  9. Builder Pattern 在 Objective-C 中的使用

    在说 Builder Pattern 之前,我们先来看看一个场景.假设我们要预定一个 iPhone 6,要 64G 的,金色的,用代码表述大概是这样 // PFX 是一个前缀,因为直接写 iPhone ...

最新文章

  1. 铜陵新松工业机器人项目_投资10亿元,茶山德威工业机器人和精密模具项目动工...
  2. android clean 框架,clean架构
  3. App.Config 和 WebConfig 特殊字符的转义码对应关系
  4. iodine免费上网——本质就是利用dns tunnel建立tcp,然后tcp proxy来实现通过访问虚拟dns0网卡来访问你的dns 授权server...
  5. cocos2d-x CCParticleSystem粒子系统
  6. stm32采集正弦波峰峰值_科研项目 | 基于STM32的永磁同步电机SVPWM控制设计
  7. python判断对象是否实例化_python中如何判断class当前有哪些实例?
  8. 十二月份找工作好找吗_注会过两门好找工作吗?好找
  9. 表单的默认提交方式_对于PHP表单提交有哪集中方式讲解
  10. 10个类手写实现 RPC 通信框架原理
  11. 自动从mysql下载到onedrive_centos7 自动上传 transmission 下载完成的文件 到 onedrive脚本...
  12. mate2 刷机 android8,华为Mate2官方原版固件rom刷机包_华为Mate2系统强刷升级包
  13. CameraLink传输协议
  14. 龙芯2F 逸珑迷你笔记本
  15. wine模拟器安装xshell
  16. 解决Eclipse中无法直接使用sun.misc.BASE64Encoder及sun.misc.BASE64Decoder的问题---gxl
  17. 和刘备相关的人(九 )
  18. js 跳转到指定位置 高德地图_第三方高德地图Javascript API
  19. Python之修改图片像素值
  20. 计算机存储器——内存、外存详解

热门文章

  1. 【python 4】python 模块
  2. pycharm上传代码到github
  3. kernel panic 和 kernel Oops
  4. 占空比50%的奇数分频
  5. ARM Neon 列子 - Vector Add
  6. 实现mvcc_一文读懂 etcd 的 mvcc 实现
  7. 生产管理erp系统源码_仁和ERP企业管理系统提高生产管理流程
  8. python文本解析_如何通过python进行文本解析?
  9. oracle轮询方式循环输出,LGWR的两种模式(POST/WAIT和POLLING)
  10. python中rim的用法_Python并发开发简介