禁止使用NSLog

Chr*_*ris 2 objective-c nslog ios

是否可以禁止使用NSLog,如果在编译时使用它会出现错误?理想情况下某种编译器标志与不允许的方法的名称?

谢谢

Mar*_*n R 11

如果你重新声明NSLog(也许也NSLogv)

void NSLog(NSString *format, ...) UNAVAILABLE_ATTRIBUTE;
void NSLogv(NSString *format, va_list args) UNAVAILABLE_ATTRIBUTE;
Run Code Online (Sandbox Code Playgroud)

在您的预编译头文件中,您收到一条很好的错误消息:

main.m:199:3: error: 'NSLog' is unavailable
                NSLog(@"%@", s1);
                ^

您甚至可以提供自定义错误消息(可在Clang文档的已弃用和不可用属性的消息中找到):

void NSLog(NSString *format, ...) __attribute__((unavailable("You should not do this!")));
Run Code Online (Sandbox Code Playgroud)

main.m:202:3: error: 'NSLog' is unavailable: You should not do this!
                NSLog(@"%@", s1);
                ^