断言NSAssert()和NSParameterAssert区别和用处
2017-06-26 09:44
375 查看
NSAssert和assert是断言,主要的差别是assert在断言失败的时候只是简单的终止程序,而NSAssert会报告出错误信息并且打印出来.所以尽管的使用NSAssert,可以不去使用assert.
iOS中用的最多的是两对断言, NSAssert/NSCAssert 和 NSParameterAssert/NSCparameterAssert.
要知道他们的区别,我们先来看看他们定义.
[objc] view
plain copy
#if !defined(NS_BLOCK_ASSERTIONS)
#if !defined(_NSAssertBody)
#define NSAssert(condition, desc, ...) \
do { \
__PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \
if (!(condition)) { \
[[NSAssertionHandler currentHandler] handleFailureInMethod:_cmd \
object:self file:[NSString stringWithUTF8String:__FILE__] \
lineNumber:__LINE__ description:(desc), ##__VA_ARGS__]; \
} \
__PRAGMA_POP_NO_EXTRA_ARG_WARNINGS \
} while(0)
#endif
#if !defined(_NSCAssertBody)
#define NSCAssert(condition, desc, ...) \
do { \
__PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \
if (!(condition)) { \
[[NSAssertionHandler currentHandler] handleFailureInFunction:[NSString stringWithUTF8String:__PRETTY_FUNCTION__] \
file:[NSString stringWithUTF8String:__FILE__] \
lineNumber:__LINE__ description:(desc), ##__VA_ARGS__]; \
} \
__PRAGMA_POP_NO_EXTRA_ARG_WARNINGS \
} while(0)
#endif
NSAssert/NSCAssert 两者的差别通过定义可以看出来,
前者是适合于Objective-C的方法,_cmd 和 self 与运行时有关. 后者是适用于C的函数.
NSParameterAssert/NSCparameterAssert两者的区别也是前者适用于Objective-C的方法,后者适用于C的函数.
NSAssert/NSCAssert 和 NSParameterAssert/NSCparameterAssert 的区别是前者是所有断言, 后者只是针对参数是否存在的断言, 所以可以先进行参数的断言,确认参数是正确的,再进行所有的断言,确认其他原因.
NSAssert的用法
[objc] view
plain copy
int a = 4;
NSAssert(a == 5, @"a must equal to 5"); //第一个参数是条件,如果第一个参数不满足条件,就会记录和打印第二个参数
[objc] view
plain copy
//回记录并打印断言日志
*** Assertion failure in -[AppDelegate application:didFinishLaunchingWithOptions:], /Users/admin/Desktop/storyboard/storyboard/AppDelegate.m:36
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'a must equal to 5
NSParameterAssert的用法
[objc] view
plain copy
- (void)assertWithPara:(NSString *)str
{
NSParameterAssert(str); //只需要一个参数,如果参数存在程序继续运行,如果参数为空,则程序停止打印日志
//further code ...
}
[objc] view
plain copy
// 如果str 为空则有如下类似的日志
*** Assertion failure in -[AppDelegate assertWithPara:], /Users/admin/Desktop/storyboard/storyboard/AppDelegate.m:45<pre name="code" class="objc">*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: str'
Xcode 已经默认将release环境下的断言取消了, 免除了忘记关闭断言造成的程序不稳定.
NSAssertionHandler:自定义处理方法,程序不会直接崩溃
NSAssertionHandler实例是自动创建的,用于处理错误断言。如果NSAssert和NSCAssert条件评估为错误,会向NSAssertionHandler实例发送一个表示错误的字符串。每个线程都有它自己的NSAssertionHandler实例。
自定义NSAssertionHandler的子类
[objc] view
plain copy
@interface MyAssertHandler : NSAssertionHandler
@end
[objc] view
plain copy
#import "MyAssertHandler.h"
@implementation MyAssertHandler
//处理Objective-C的断言
- (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);
}
//处理C的断言
- (void)handleFailureInFunction:(NSString *)functionName file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format,...
{
NSLog(@"NSCAssert Failure: Function (%@) in %@#%li", functionName, fileName, (long)line);
}
@end
给线程添加处理类
[objc] view
plain copy
NSAssertionHandler *myHandler = [[MyAssertHandler alloc] init];
//给当前的线程
[[[NSThread currentThread] threadDictionary] setValue:myHandler
forKey:NSAssertionHandlerKey];
实现这些以后,程序能够获得断言失败后的信息,但是程序有可能继续运行,不会强制退出程序.
原文博客http://blog.csdn.net/antjumper/article/details/38975769;http://blog.csdn.net/sunjie886/article/details/53375968
iOS中用的最多的是两对断言, NSAssert/NSCAssert 和 NSParameterAssert/NSCparameterAssert.
要知道他们的区别,我们先来看看他们定义.
[objc] view
plain copy
#if !defined(NS_BLOCK_ASSERTIONS)
#if !defined(_NSAssertBody)
#define NSAssert(condition, desc, ...) \
do { \
__PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \
if (!(condition)) { \
[[NSAssertionHandler currentHandler] handleFailureInMethod:_cmd \
object:self file:[NSString stringWithUTF8String:__FILE__] \
lineNumber:__LINE__ description:(desc), ##__VA_ARGS__]; \
} \
__PRAGMA_POP_NO_EXTRA_ARG_WARNINGS \
} while(0)
#endif
#if !defined(_NSCAssertBody)
#define NSCAssert(condition, desc, ...) \
do { \
__PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \
if (!(condition)) { \
[[NSAssertionHandler currentHandler] handleFailureInFunction:[NSString stringWithUTF8String:__PRETTY_FUNCTION__] \
file:[NSString stringWithUTF8String:__FILE__] \
lineNumber:__LINE__ description:(desc), ##__VA_ARGS__]; \
} \
__PRAGMA_POP_NO_EXTRA_ARG_WARNINGS \
} while(0)
#endif
NSAssert/NSCAssert 两者的差别通过定义可以看出来,
前者是适合于Objective-C的方法,_cmd 和 self 与运行时有关. 后者是适用于C的函数.
NSParameterAssert/NSCparameterAssert两者的区别也是前者适用于Objective-C的方法,后者适用于C的函数.
NSAssert/NSCAssert 和 NSParameterAssert/NSCparameterAssert 的区别是前者是所有断言, 后者只是针对参数是否存在的断言, 所以可以先进行参数的断言,确认参数是正确的,再进行所有的断言,确认其他原因.
NSAssert的用法
[objc] view
plain copy
int a = 4;
NSAssert(a == 5, @"a must equal to 5"); //第一个参数是条件,如果第一个参数不满足条件,就会记录和打印第二个参数
[objc] view
plain copy
//回记录并打印断言日志
*** Assertion failure in -[AppDelegate application:didFinishLaunchingWithOptions:], /Users/admin/Desktop/storyboard/storyboard/AppDelegate.m:36
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'a must equal to 5
NSParameterAssert的用法
[objc] view
plain copy
- (void)assertWithPara:(NSString *)str
{
NSParameterAssert(str); //只需要一个参数,如果参数存在程序继续运行,如果参数为空,则程序停止打印日志
//further code ...
}
[objc] view
plain copy
// 如果str 为空则有如下类似的日志
*** Assertion failure in -[AppDelegate assertWithPara:], /Users/admin/Desktop/storyboard/storyboard/AppDelegate.m:45<pre name="code" class="objc">*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: str'
Xcode 已经默认将release环境下的断言取消了, 免除了忘记关闭断言造成的程序不稳定.
NSAssertionHandler:自定义处理方法,程序不会直接崩溃
NSAssertionHandler实例是自动创建的,用于处理错误断言。如果NSAssert和NSCAssert条件评估为错误,会向NSAssertionHandler实例发送一个表示错误的字符串。每个线程都有它自己的NSAssertionHandler实例。
自定义NSAssertionHandler的子类
[objc] view
plain copy
@interface MyAssertHandler : NSAssertionHandler
@end
[objc] view
plain copy
#import "MyAssertHandler.h"
@implementation MyAssertHandler
//处理Objective-C的断言
- (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);
}
//处理C的断言
- (void)handleFailureInFunction:(NSString *)functionName file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format,...
{
NSLog(@"NSCAssert Failure: Function (%@) in %@#%li", functionName, fileName, (long)line);
}
@end
给线程添加处理类
[objc] view
plain copy
NSAssertionHandler *myHandler = [[MyAssertHandler alloc] init];
//给当前的线程
[[[NSThread currentThread] threadDictionary] setValue:myHandler
forKey:NSAssertionHandlerKey];
实现这些以后,程序能够获得断言失败后的信息,但是程序有可能继续运行,不会强制退出程序.
原文博客http://blog.csdn.net/antjumper/article/details/38975769;http://blog.csdn.net/sunjie886/article/details/53375968
相关文章推荐
- iOS-----程序异常处理----- 断言NSAssert()和NSParameterAssert区别和用处
- 使用断言NSAssert()和NSParameterAssert调试程序错误
- 使用断言NSAssert()和NSParameterAssert调试程序错误
- 使用断言NSAssert()和NSParameterAssert调试程序错误
- 使用断言NSAssert()和NSParameterAssert调试程序错误
- assert断言的用处
- 断言assert在debug版本与release版本中的区别
- python3 assert 断言的用法 (区别于python2)
- 断言 NSAssert assert NS_BLOCK_ASSERTIONS
- NSAssert与assert断言
- 【Java】断言中assertNotEquals与assertNotSame的区别
- NSAssert NSCAssert NSParameterAssert
- NSAssert NSCAssert NSParameterAssert
- 浅析VO、DTO、DO、PO的概念、区别和用处
- 上层判断,底层断言!(if vs assert)
- 探索C++0x: 1. 静态断言(static_assert)
- assert断言函数的使用
- assert断言
- assert 断言
- C/C++ 中的assert()宏 断言机制