问题描述
我通过在运行时选择最新的 API 来支持 10.4+:
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];
在这种情况下,10.5 及更高版本将使用 removeItemAtPath:error:
,10.4 将使用 removeFileAtPath:handler:
.很好,但我仍然收到旧方法的编译器警告:
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]
是否有if([... RespondsToSelector:@selector(...)]){ ... } else { ... }
的语法提示编译器(Clang)不要在该行发出警告?
Is there a syntax of if([… respondsToSelector:@selector(…)]){ … } else { … }
that hints the compiler (Clang) to not warn on that line?
如果没有,有没有办法标记该行以被 -Wdeprecated-declarations
忽略?
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.
推荐答案
我发现 Clang Compiler User's Manual 中的一个示例让我忽略警告:
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
}
这篇关于抑制“'...' 已被弃用";使用 RespondsToSelector 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!