Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually deprecate members

Unlike Objective-C, Swift has no preprocessor, so is there still a way to manually deprecate members of a class?

I am looking for something similar to this:

-(id)method __deprecated; 
like image 620
Atomix Avatar asked Aug 20 '14 12:08

Atomix


People also ask

How do you mark deprecated?

Starting with J2SE 5.0, you deprecate a class, method, or field by using the @Deprecated annotation. Additionally, you can use the @deprecated Javadoc tag tell developers what to use instead. Using the annotation causes the Java compiler to generate warnings when the deprecated class, method, or field is used.

How do you mark a method as deprecated in Swift?

Marking a method as deprecated or obsoleted You could mark the specific method if it's used from an SDK as deprecated and obsoleted as follows: @available(iOS, deprecated: 12, obsoleted: 13, message: "We no longer show an app introduction on iOS 14 and up") func launchAppIntroduction() { // .. }

How do you mark a class as deprecated in C#?

You need to use the [Obsolete] attribute. Example: [Obsolete("Not used any more", true)] public class MyDeprecatedClass { //... }

What is depreciation in programming?

In computer software standards and documentation, the term deprecation is used to indicate discouragement of usage of a particular software feature, usually because it has been superseded by a newer/better version.


2 Answers

You can use the Available tag, for example :

@available(*, deprecated) func myFunc() {      // ... } 

Where * is the platform (iOS, iOSApplicationExtension, macOS, watchOS, tvOS, * for all, etc.).

You can also specify the version of the platform from which it was introduced, deprecated, obsoleted, renamed, and a message :

@available(iOS, deprecated:6.0) func myFunc() {      // calling this function is deprecated on iOS6+ }  Or  @available(iOS, deprecated: 6.0, obsoleted: 7.0, message: "Because !") func myFunc() {     // deprecated from iOS6, and obsoleted after iOS7, the message "Because !" is displayed in XCode warnings } 

If your project targets multiple platforms, you can use several tags like so :

@available(tvOS, deprecated:9.0.1) @available(iOS, deprecated:9.1) @available(macOS, unavailable, message: "Unavailable on macOS") func myFunc() {     // ... } 

More details in the Swift documentation.

like image 149
Axel Guilmin Avatar answered Sep 28 '22 07:09

Axel Guilmin


Starting Swift 3 and Swift 4, the version number is optional. You can now simply type:

@available(*, deprecated) func foo() {     // ... } 

Or if you want a message go along with it:

@available(*, deprecated, message: "no longer available ...") func foo() {     // ... } 
like image 25
Yuchen Avatar answered Sep 28 '22 07:09

Yuchen