快速运行已弃用的代码会怎样? [英] What happens when you run deprecated code in swift?

查看:76
本文介绍了快速运行已弃用的代码会怎样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在iOS 10中,存在 UserNotifications 框架。在此之前,您只需要使用:

For example in iOS 10 there's UserNotifications framework. Prior to that you just have to use:

let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
 application.registerUserNotificationSettings(settings)
 application.registerForRemoteNotifications()

(我在iOS 10上运行了上面的代码,什么都没发生)。

(I ran the code above on iOS 10 and nothing happened).

我要获得更好的调试理解

My generic question is: would running deprecated syntax crash all the time, just do nothing (as now) or how it treats is unknown?

推荐答案


治疗方式未知

基本上是正确的。

在UILocalNotification世界中,经验表明,如果您针对iOS 10进行链接,则不会崩溃。

Basically, that's right. It wouldn't crash, probably, but it isn't guaranteed to work either.

您的代码将无法正常工作。您必须切换到新的UNUserNotificationCenter代码。

In the case of the UILocalNotification world, experience shows that if you link against iOS 10 your code won't work properly. You must switch over to the new UNUserNotificationCenter code.

但这不是由规则决定的,而是由实验决定的。

But this is determined not by rule but by experimentation.

一个可靠的通用规则是,如果当前iOS版本不赞成使用某些东西,请不要使用它。

The one reliable general rule is that if something is deprecated for the current iOS version, don't use it. That is precisely what deprecation means.

当然,如果您希望代码针对iOS 10链接但同时在iOS 10和iOS 9上运行,则可能必须包含两组完全不同的代码集,以利用Swift的 if #available()语法来响应我们实际运行的系统。在这种情况下,您必须这样做才能管理本地通知。但这就是进步的代价。 Apple(故意?)使您很难编写向后兼容的代码。

Of course, if you want your code to link against iOS 10 but to run on both iOS 10 and iOS 9, say, you might have to include two completely different sets of code, taking advantage of Swift's if #available() syntax to respond to what system we're actually running under. In this particular case, you would have to do that in order to manage local notifications. But that is the price of progress. Apple (deliberately?) makes it hard for you to write backwards-compatible code.

这篇关于快速运行已弃用的代码会怎样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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