NSAssert(断言)

NSAssert()是一个宏,用于开发阶段调试程序中的Bug,通过为NSAssert()传递条件表达式来断定是否属于Bug,满足条件返回真值,程序继续运行,如果返回假值,则抛出异常,并且可以自定义异常描述。

NSAssert是一系列宏,主要方法有以下几个

/** `condition`条件语句,当条件为假时,程序会在这一行崩溃并且打印`desc`信息 */
#define NSAssert(condition, desc, ...)
#define NSAssert1(condition, desc, arg1)
...
#define NSAssert5(condition, desc, arg1, arg2, arg3, arg4, arg5) 

#define NSParameterAssert(condition) NSAssert((condition), @"Invalid parameter not satisfying: %@", @#condition)

------------------------------------------------

/** `NSCAssert`含义和`NSAssert`相同 */
#define NSCAssert(condition, desc, ...)
#define NSCAssert1(condition, desc, arg1)
...
#define NSCAssert5(condition, desc, arg1, arg2, arg3, arg4, arg5) 

#define NSCParameterAssert(condition) NSCAssert((condition), @"Invalid parameter not satisfying: %@", @#condition)

// 注意,NSAssert 适用于OC的断言,而 NSCAssert 适用于C函数的断言
关于NSAssert和assert 区别

NSAssert和assert都是断言,主要的差别是assert在断言失败的时候只是简单的终止程序,而NSAssert会报告出错误信息并且打印出来.所以只使用NSAssert就好,可以不去使用assert

NSAssert/NSCAssert 和 NSParameterAssert/NSCparameterAssert 的区别

前者是针对条件断言, 后者只是针对参数是否存在的断言, 调试时候可以结合使用,先判断参数,再进一步断言,确认原因

NSAssert 使用
NSInteger i = 10;

// 第一个参数是条件,如果条件为假,就会记录并打印后面的字符串
NSAssert(i > 10, @"i 实际上 <= 10");

运行程序崩溃并且看到打印错误信息:

 *** Assertion failure in -[ViewController viewDidLoad], /Users/ZFeng/Desktop/temp/temp/
 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'i 实际上 <= 10'
NSParameterAssert 使用
- (void)testNSParameterAssert:(NSString *)str
{
    // 参数存在程序继续运行,如果参数为空,则程序停止打印日志
    NSParameterAssert(str);

    // do somthing
}

我们调用方法并且给str传一个空值:

[self testNSParameterAssert:nil];

运行程序崩溃并且看到打印错误信息:

*** Assertion failure in -[ViewController testNSParameterAssert:], /Users/ZFeng/Desktop/temp/temp/ViewController.m:28
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: str'
NSAssertionHandler 断言处理

NSAssertionHandler实例是自动创建的,用于处理错误断言。如果 NSAssert和NSCAssert条件评估为错误,会向 NSAssertionHandler实例发送一个表示错误的字符串。每个线程都有它自己的NSAssertionHandler实例。
我们可以自定义处理方法,从而使用断言的时候,控制台输出错误,但是程序不会直接崩溃。

// 处理C的断言
- (void)handleFailureInFunction:(NSString *)functionName file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format, ...
{
    NSLog(@"NSCAssert Failure: Function (%@) in %@#%li", functionName, fileName, (long)line);
}

// 处理OC的断言
- (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format, ...
{
    NSLog(@"NSAssert Failure: Method %@ for object %@ in %@#%li", NSStringFromSelector(selector), object, fileName, (long)line);
}

使用我们自己的断言进行处理:

// 自定义的断言处理
NSAssertionHandler *myHandler = [[ZFAssertionHandler alloc] init];
[[[NSThread currentThread] threadDictionary] setValue:myHandler forKey:NSAssertionHandlerKey];

自定义NSAssertionHandler后,程序能够获得断言失败后的信息,但是程序可以继续运行,不会强制退出程序。

Xcode 已经默认将release环境下的断言取消了, 免除了忘记关闭断言造成的程序不稳定. 所以不用担心 在开发时候大胆使用。

xcode断言设置
参考了
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,837评论 18 139
  • NSAssert与assert NSAssert和assert是断言,主要的差别是assert在断言失败的时候只是...
    GoCac阅读 307评论 0 1
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,926评论 6 342
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,861评论 25 708
  • 深夜,路灯灯光浅淡,风雨交织呼喊。不眠人,静卧,沁粒粒凉雨,迎习习清风。风为无形,却能化有形。雨为有形,却能遁无形。
    像只企鹅一样阅读 199评论 0 0