How to manually deprecate members

IosObjective CSwiftC PreprocessorDeprecated

Ios Problem Overview


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;

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

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() {
    // ...
}

Solution 3 - Ios

You can use this to auto-fix you entrys with your new func

@available(*, deprecated, renamed: "myNewFunc")
func myOldFunc() {
   // ...
}

func myNewFunc() {
   // ...
}

Instead of * you can use swift , for the swift Version number.

Deprecated functions generate warnings but can still be called. (Warning)

Obsolete functions stop it from being called entirely. (Error)

@available(swift, deprecated: 4.0, obsoleted: 4.2, message: "This will be removed in v4.2, please migrate to ...")

or use other Options like iOS, macOS, watchOS, tvOS ...

Solution 4 - Ios

iOS deprecate

@available(iOS, deprecated:7.0, obsoleted: <ObsoletedVersion>, renamed: "myFuncNew", message: "Please use new method - myFuncNew()")
func myFuncOld() {
    //logic
}

If deployment target[About] == 9.0 and

  • <ObsoletedVersion> == 10.0 - warning

    enter image description here

  • <ObsoletedVersion> == 8.0 - compile error

    enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAtomixView Question on Stackoverflow
Solution 1 - IosAxel GuilminView Answer on Stackoverflow
Solution 2 - IosYuchenView Answer on Stackoverflow
Solution 3 - IosSkyborgView Answer on Stackoverflow
Solution 4 - IosyoAlex5View Answer on Stackoverflow