#import <Foundation/Foundation.h>
@interface YourClass : NSObject
// 普通方法
- (void)commonMethod;
// 仅在DEBUG模式下编译的方法
#ifdef DEBUG
- (void)debugMethod;
#endif
@end
@implementation YourClass
- (void)commonMethod {
NSLog(@"This is a common method.");
}
#ifdef DEBUG
- (void)debugMethod {
NSLog(@"This is a debug method.");
}
#endif
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
YourClass *obj = [[YourClass alloc] init];
[obj commonMethod];
#ifdef DEBUG
[obj debugMethod];
#endif
}
return 0;
}
用到的预处理指令及其作用
#ifdef
:判断某个宏是否已经定义。在上述代码中,#ifdef DEBUG
用于判断 DEBUG
宏是否已定义,如果已定义(即在DEBUG模式下),则编译 #ifdef DEBUG
和 #endif
之间的代码,这里是 debugMethod
的声明和实现。
#endif
:与 #ifdef
配对使用,用于结束 #ifdef
条件判断范围内的代码块。