xcode Objective-C 中的 NS_BLOCK_ASSERTIONS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6445222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NS_BLOCK_ASSERTIONS in Objective-C
提问by John Muchow
I am using NSAssert() calls within an iPhone application and my understanding from the Apple docs is that assertions are not compiled into the code if NS_BLOCK_ASSERTIONS is defined.
我在 iPhone 应用程序中使用 NSAssert() 调用,我从 Apple 文档中了解到,如果定义了 NS_BLOCK_ASSERTIONS,则断言不会编译到代码中。
To turn off assertions, in a header file I declare: #define NS_BLOCK_ASSERTIONS
要关闭断言,我在头文件中声明:#define NS_BLOCK_ASSERTIONS
However, the assert code still seems to run.
但是,断言代码似乎仍在运行。
Is there something I am missing here?
有什么我在这里想念的吗?
Thanks
谢谢
John
约翰
回答by puzzle
If you created your Xcode project based on one of the standard templates, the Cocoa headers (including NSException.h
which contains the NSAssert
macros) will get preprocessed before any other files in the project. A #define NS_BLOCK_ASSERTIONS
in any of the project's header or implementation files therefore has no effect on the NSAssert
macros.
如果您基于标准模板之一创建了 Xcode 项目,则 Cocoa 标头(包括NSException.h
包含NSAssert
宏的标头)将在项目中的任何其他文件之前进行预处理。因此#define NS_BLOCK_ASSERTIONS
,任何项目的头文件或实现文件中的A对NSAssert
宏都没有影响。
Try putting NS_BLOCK_ASSERTIONS
into the preprocessor macros of your target or even project (for the release configuration only):
尝试放入NS_BLOCK_ASSERTIONS
目标甚至项目的预处理器宏(仅适用于发布配置):
Or put #define NS_BLOCK_ASSERTIONS
into the prefix (.pch) header before the #import <Cocoa/Cocoa.h>
or #import <Foundation/Foundation.h>
lines.
或者#define NS_BLOCK_ASSERTIONS
在#import <Cocoa/Cocoa.h>
或#import <Foundation/Foundation.h>
行之前放入前缀(.pch)标题中。
回答by blackjacx
As @dwsolberg mentioned, Xcode has a new build setting called ENABLE_NS_ASSERTIONS
. For new projects its value for the release configuration is set to NO
and for all other configurations to YES
. You can use this setting as well as the widely used NS_BLOCK_ASSERTIONS
approach which is still valid in Xcode 6.
正如@dwsolberg 提到的,Xcode 有一个名为ENABLE_NS_ASSERTIONS
. 对于新项目,其发布配置的值设置为NO
,所有其他配置的值设置为YES
。您可以使用此设置以及广泛使用的NS_BLOCK_ASSERTIONS
方法,该方法在 Xcode 6 中仍然有效。
Assertions are a tool to track down bugs during development time and should never fire in productive code! Also exceptions should be used only if it is absoloutely neccessary, i.e. if something went so damn wrong that the programm is not able to continue execution. The Cocoa way is to give critical methods a boolean return value and parametrize them with an error object that can be set inside the method and can be used outside if the return value is NO
.
断言是在开发期间追踪错误的工具,不应在生产代码中触发!此外,只有在绝对必要的情况下才应该使用异常,即,如果某些事情发生得太该死的错误以至于程序无法继续执行。Cocoa 的方法是给关键方法一个布尔返回值,并使用一个错误对象对它们进行参数化,该对象可以在方法内部设置,如果返回值为 ,则可以在外部使用NO
。
Hope that helps some folks ;-)
希望能帮助一些人;-)