Deprecating code in Swift
Deprecating code is a common thing to do in large codebases. This will come in handy when you are creating frameworks, or changing a old function to a new one. In this case we will use the swift version to deprecate the code. Lets get started.
@available(swift, deprecated: 4.2, obsoleted: 5.0, message: "Deprecated code")
The line above when placed above any code will deprecate it. If you place this above a function when the function is called a warning or error will appear. From the line above we can see that if you are running Swift 4.2 you will get a warning, and if you are running Swift 5.0 then you will get a error. The error and warning will display the message indicated in the line of code above. Now lets see a warning deprecated message.
@available(swift, deprecated: 5.0, obsoleted: 5.1, message: "Deprecated code")
Because my Swift version is 5.0 we get a warning. If my swift version was 5.1 I would get a error. As you can see both errors, and. warnings display the message we indicated in the parameters of the line above. Now lets show you how this looks when doing the same to a variable. You can deprecate almost anything. I like to use this for API keys that are about to expire for example. A variable that contains a api key. Here is how that will look.
As you can see we get an error when printing the variable above. Thats the end of this tutorial. Deprecating code is useful, however don’t get in the habit of doing this unless you are working on a large codebase with multiple people. Sometimes you are not allowed to touch parts of a codebase, and that’s when this comes in handy.