如何使用 #ifdef 指令生成调试代码。 Objective-c
我想问一个关于使用 #ifdef 指令进行开发的问题。
我想在 Objective-C 中制作一些代码仅用于调试使用,例如:
在主函数中执行此操作:
#define DEBUG_LEVEL
在我的界面中执行此操作:
#ifdef DEBUG_LEVEL
BOOL editorIsDragged;
BOOL editorIsSelected;
#endif
...其他属性
#ifdef #DEBUG_LEVEL
@property (nonatomic, readwrite) BOOL editorIsDragged;
@property (nonatomic, readwrite) BOOL editorIsSelected;
#endif
然后在我的实现中执行此操作:
#ifdef #DEBUG_LEVEL
@synthetize editorIsDragged, editorIsSelected;
#endif
但我收到错误,因为在Synthetize editorIsDragged
和 editorIsSelected
未定义。 如果我尝试定义自己的 setter/getter 方法,我会收到相同的错误,因为 XCode 不存在我的变量(editorIsDragged
和 editorIsSelected
)!
我在 C 中使用这种方法只编写调试代码,但在 obj-c 中我必须使用什么?
谢谢你!
i want to make a question about developing using #ifdef directive.
I want do make some code in objective-c only for debug use, for example:
in main function do this:
#define DEBUG_LEVEL
in my interface do this:
#ifdef DEBUG_LEVEL
BOOL editorIsDragged;
BOOL editorIsSelected;
#endif
.... other property
#ifdef #DEBUG_LEVEL
@property (nonatomic, readwrite) BOOL editorIsDragged;
@property (nonatomic, readwrite) BOOL editorIsSelected;
#endif
then in my implementation do this:
#ifdef #DEBUG_LEVEL
@synthetize editorIsDragged, editorIsSelected;
#endif
but i receve an error because in synthetize editorIsDragged
and editorIsSelected
are not defined.
If i try to define my own setter/getter method I receive the same error, because my vars (editorIsDragged
and editorIsSelected
) does not exist for XCode!
I in C use this method for write only debug code, but in obj-c what i must use??
thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
难道你不应该将 #define DEBUG_LEVEL 放在一个头文件中,该文件包含在所有需要知道它的地方吗?就像在构建设置中设置或放入 *.pch 一样。我不确定这是否是一个拼写错误,但您在此处的代码中也有#define #DEBUG_LEVEL(参见第二个散列?)。
Shouldn't you put your #define DEBUG_LEVEL in a header file that gets included in all places that needs to know it? Like setting in in the build settings or putting in the *.pch. And I'm not sure if that a typo but you have also #define #DEBUG_LEVEL (see the second hash?) in the code here.