Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when you run deprecated code in swift?

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()

(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?

I'm asking to get a better debugging understanding.

like image 924
mfaani Avatar asked Oct 29 '25 01:10

mfaani


2 Answers

how it treats is unknown

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

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.

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.

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.

like image 69
matt Avatar answered Oct 31 '25 17:10

matt


Deprecated means Apple may remove it anytime in the future. It may work for a few more years but one day it will crash and you can't say you have not been warned. If you are writing new code, why start with something that's already obsolete? If this is in your existing code, consider updating it to the new API when have a chance.

A deprecation warning is exactly the tool that gave you time to do this.

like image 30
Code Different Avatar answered Oct 31 '25 15:10

Code Different