【发布时间】:2017-01-08 02:44:21
【问题描述】:
尝试使用 NS_BLOCK_ASSERTIONS #undef,但看起来设置中的 Release 默认选项被粗暴地焊接到构建过程中。诀窍是只使用 .m 源代码文件指令(可能是编译指示)来禁用它,因为我处于无法控制 XCode 项目设置的情况,并且它们在发布版本中被设置为我的默认设置。
#undef NS_BLOCK_ASSERTIONS
#import <Foundation/Foundation.h>
#include <stdio.h>
@interface LoggingAssertionHandler : NSAssertionHandler
@end
@implementation LoggingAssertionHandler
- (void)handleFailureInMethod:(SEL)selector
object:(id)object
file:(NSString *)fileName
lineNumber:(NSInteger)line
description:(NSString *)format, ...
{
NSString *failureMessageDescription = [NSString stringWithFormat:@"NSAssert Failure: Method %@ for object %@ in %@#%li. Reason: \"%@\"", NSStringFromSelector(selector), object, fileName, (long)line, format];
printf("%s\n", [failureMessageDescription UTF8String]);
}
- (void)handleFailureInFunction:(NSString *)functionName
file:(NSString *)fileName
lineNumber:(NSInteger)line
description:(NSString *)format, ...
{
NSString *failureMessageDescription = [NSString stringWithFormat:@"NSCAssert Failure: Function (%@) in %@#%li. Reason: \"%@\"", functionName, fileName, (long)line, format];
printf("%s\n", [failureMessageDescription UTF8String]);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSAssertionHandler *assertionHandler = [[LoggingAssertionHandler alloc] init];
[[[NSThread currentThread] threadDictionary] setValue:assertionHandler forKey:NSAssertionHandlerKey];
NSCAssert(true == false, @"Impossible.");
NSLog(@"Hello, World!");
}
return 0;
}
【问题讨论】:
-
我使用的 XCode 版本是 8.2.1。
-
您能否详细说明 Xcode 项目设置的细节超出您的能力范围?您是否不允许触摸目标的项目设置?你不能设置 Xcode 配置文件吗?这方面的信息将有助于为您提供更好的答案。
标签: objective-c xcode compiler-flags