Suppressing "'…' is deprecated" when using respondsToSelector

s4y picture s4y · Dec 14, 2009 · Viewed 21.4k times · Source

I'm supporting 10.4+ by picking the most-current API at runtime:

if ([fileManager respondsToSelector:@selector(removeItemAtPath:error:)])
    [fileManager removeItemAtPath:downloadDir error:NULL];
else
    [fileManager removeFileAtPath:downloadDir handler:nil];

In this case, 10.5 and up will use removeItemAtPath:error: and 10.4 will use removeFileAtPath:handler:. Great, but I still get compiler warnings for the old methods:

warning: 'removeFileAtPath:handler:' is deprecated [-Wdeprecated-declarations]

Is there a syntax of if([… respondsToSelector:@selector(…)]){ … } else { … } that hints the compiler (Clang) to not warn on that line?

If not, is there a way to tag that line to be ignored for -Wdeprecated-declarations?


After seeing some of the answers, let me clarify that confusing the compiler into not knowing what I'm doing is not a valid solution.

Answer

s4y picture s4y · Dec 14, 2009

I found an example in the Clang Compiler User's Manual that lets me ignore the warning:

if ([fileManager respondsToSelector:@selector(removeItemAtPath:error:)]) {
    [fileManager removeItemAtPath:downloadDir error:NULL];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    [fileManager removeFileAtPath:downloadDir handler:nil];
#pragma clang diagnostic pop
}