xcode中的弃用警告以及如何处理弃用 [英] deprecated warnings in xcode and how to handle deprecation

查看:627
本文介绍了xcode中的弃用警告以及如何处理弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if ([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)])
     {[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];} //post-iOS6.0
else {[self dismissModalViewControllerAnimated:YES];} //pre-iOS6.0

我正在响应选择器(上面)代码来处理不推荐使用的方法。这样我的应用程序与旧版本的iOS兼容,但我在代码中收到警告:'dismissModalViewControllerAnimated:'已被弃用:首先在iOS 6.0中弃用
我个人不喜欢任何警告我的代码,但更重要的是,我在某处读到苹果会在你的代码中抱怨警告。

I'm doing the responds to selector (above) code to handle deprecated methods. That way my app is compatible with older versions of iOS, but I'm getting warnings in my code stating: "'dismissModalViewControllerAnimated:' is deprecated: first deprecated in iOS 6.0" I personally don't like any warning in my code, but more importantly, I read somewhere that apple will complain about warnings in your code.

1)Apple会在你的代码中抱怨警告吗?

1) Will Apple complain about warnings in your code?

2)我是否正确处理弃用方法?

2) Am I handling deprecated methods correctly?

3)是否可以关闭弃用的方法方法警告?

3) Is there way to turn deprecated method method warnings off?

推荐答案


  1. Apple不知道您通过代码收到的任何编译时警告。

  1. Apple is unaware of any compile-time warnings you receive with your code.

是的,您正在正确处理此做法。显然,在这种情况下,如果您在5.0之前支持iOS,则只需要完成这项工作。但是,一般来说,测试方法是否可以被调用然后调用适当的再现的技术是完全正确的。

Yes, you are handling this practice correctly. Clearly, in this case, you only need to go through this effort if you're supporting iOS prior to 5.0. But, in general, the technique for testing whether a method can be invoked and then calling the appropriate rendition is absolutely correct.

如果你想关闭它警告,您只需暂时禁止警告,然后使用相应的 #pragma 语法将其重新打开:

If you want to turn off the warning, you would simply temporarily suppress the warning and then turn it back on afterwards using the appropriate #pragma syntax:

if ([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)])
{
    //post-iOS6.0
    [self dismissViewControllerAnimated:YES completion:nil];
} 
else
{
    // pre-iOS6.0
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    [self dismissModalViewControllerAnimated:YES];
#pragma clang diagnostic pop
}

顺便说一下,如果你想要的话要知道 -W 代码是针对您的特定警告的,请转到Log Navigator,选择包含警告的最新版本,然后展开日志,您将会在那里看到:

By the way, if you want to know what the -W code is for your particular warning, go to your Log Navigator, select the recent build that included the warning, and expand the log, and you'll see it there:

另请注意,尽管您可以像我上面所说的那样抑制警告,但在实践中,您很少需要这样做所以。使用您的示例,如果您的项目的iOS部署目标是4.3,则不会收到警告。如果您的部署目标是6.0或更高,您将收到该警告,但是再次,您可能不需要此条件代码来调用 dismissModalViewControllerAnimated ,因为有效的iOS 5.0 ,你总是可以使用 dismissViewControllerAnimated

Also note that while you could suppress the warning like I've illustrated above, in practice, you rarely need to do so. Using your example, if your project's iOS deployment target was 4.3, you wouldn't get the warning. And if your deployment target was 6.0 or higher, you'd get that warning, but then again, you probably wouldn't need this conditional code to call dismissModalViewControllerAnimated because effective iOS 5.0, you can always use dismissViewControllerAnimated.

你唯一需要在代码中取消此警告的时间如果您有源代码,将来包含在项目中,您不知道部署目标是什么。使用您的示例,如果您不知道上述代码是否将包含在具有4.3部署目标或5.0+部署目标的项目中。在这种情况下,这种语法非常有用。但是,再次,我还可以在 __ IPHONE_OS_VERSION_MIN_REQUIRED 上使用条件检查,例如:

The only time that you'd need to suppress this warning in your code is if you have source code, to be included in projects in the future, for which you don't know what the deployment target will be. Using your example, if you don't know whether the above code will be included in a project with a 4.3 deployment target or a 5.0+ deployment target. In that case, this syntax is quite useful. But, then again, I you could also use conditional checks on __IPHONE_OS_VERSION_MIN_REQUIRED, e.g.:

#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_5_0
    if ([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)])
    {
        //post-iOS5.0
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else
    {
        // pre-iOS5.0
        [self dismissModalViewControllerAnimated:YES];
    }
#else
    [self dismissViewControllerAnimated:YES completion:nil];
#endif

这篇关于xcode中的弃用警告以及如何处理弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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