文章目录
  1. 1. 引用

在调试时使用断言,发布模式设置如下关闭断言
@关闭断言

也可以定义宏来关闭断言

1
put #define NS_BLOCK_ASSERTIONS into the prefix (.pch) header before the #import <Cocoa/Cocoa.h> or #import <Foundation/Foundation.h> lines.

当然,如果我们自定义了 NSAssertionHandler
将自定义的NSAssertionHandler加入当前线程的属性中,在当前线程中使用的NSAssert()等断言将不会崩溃程序,断言将走自定义NSAssertionHandler的回调函数,此时你可将错误信息存储

1
2
3
4
//设置线程的自定义断言属性
NSThread * thread = [[NSThread alloc]initWithTarget:self selector:@selector(Log:) object:@"打印"];
TAssertionHandler * assertionHandler = [[TAssertionHandler alloc]init];
[[thread threadDictionary] setValue:assertionHandler forKey:NSAssertionHandlerKey];

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//(当 NSAssert / NSParameterAssert 失败时调用) OC断言回调
- (void)handleFailureInMethod:(SEL)selector
object:(id)object file:(NSString *)fileName
lineNumber:(NSInteger)line
description:(nullable NSString *)format,... NS_FORMAT_FUNCTION(5,6){
NSLog(@"NSAssert Failure: Method %@ for object %@ in %@#%i", NSStringFromSelector(selector), object, fileName, line);

}
// (当 NSCAssert / NSCParameterAssert 失败时调用) C语言断言回调
- (void)handleFailureInFunction:(NSString *)functionName
file:(NSString *)fileName
lineNumber:(NSInteger)line
description:(nullable NSString *)format,... NS_FORMAT_FUNCTION(4,5){
NSLog(@"NSCAssert Failure: Function (%@) in %@#%i", functionName, fileName, line);
}

引用

NS_BLOCK_ASSERTIONS in Objective-C

NSAssertionHandler

文章目录
  1. 1. 引用