Search code examples
iosobjective-cnslog

Disallow NSLog to be used


Is it possible to disallow the use of NSLog, so that it will come up as an error if used at compile time? Ideally some sort of compiler flag with the name of the method that is disallowed?

Thanks


Solution

  • If you re-declare NSLog (and perhaps also NSLogv) as

    void NSLog(NSString *format, ...) UNAVAILABLE_ATTRIBUTE;
    void NSLogv(NSString *format, va_list args) UNAVAILABLE_ATTRIBUTE;
    

    in your precompiled header file, you get a nice error message:

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

    You can even provide a custom error message (found in Messages on deprecated and unavailable Attributes of the Clang documentation):

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