抑制“'...”被弃用“当使用responsesToSelector时 [英] Suppressing "'…' is deprecated" when using respondsToSelector

查看:159
本文介绍了抑制“'...”被弃用“当使用responsesToSelector时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过在运行时选择最新的API来支持10.4+:

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

在这种情况下,10.5及以上将使用 removeItemAtPath:error: code>和10.4将使用 removeFileAtPath:handler:。很好,但是我仍然得到旧方法的编译器警告:

 警告:'removeFileAtPath:handler:'已弃用[ -  Wdeprecated code> if([... responsesToSelector:@ ...] 

< (...)]){...} else {...}
提示编译器(Clang)不在该行上发出警告?



没有,是否有一种方法来标记要忽略的行 -Wdeprecated-declarations






看到一些答案后,让我澄清一下,让编译器不知道我在做什么不是一个有效的解决方案。

解决方案

我发现一个例子在Clang编译器用户手册中,让我忽略警告:

  if([fileManager responsesToSelector:@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诊断pop
}


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.

解决方案

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
}

这篇关于抑制“'...”被弃用“当使用responsesToSelector时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆