186

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;
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Atomix
  • 13,427
  • 9
  • 38
  • 46
  • Does this answer your question? [How to deprecate a method in Xcode](https://stackoverflow.com/questions/4924285/how-to-deprecate-a-method-in-xcode) – Lal Krishna Nov 25 '22 at 07:51

6 Answers6

305

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.

Axel Guilmin
  • 11,454
  • 9
  • 54
  • 64
80

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() {
    // ...
}
Yuchen
  • 30,852
  • 26
  • 164
  • 234
  • 1
    For Swift 2.3, version number is optional, too. – DawnSong Oct 12 '16 at 11:33
  • 3
    For Swift 2.3, the syntax seems to be `@available(*, deprecated, message = "no longer available ...")` when including a message. – Daniel Zhang Nov 23 '16 at 06:35
  • @Daniel, that's weird that Apple changes part of it and not the rest and calling it 2.3. Sign, I will update the answer again, thanks for the note! – Yuchen Nov 23 '16 at 14:54
6

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 ...

Skyborg
  • 854
  • 13
  • 13
3

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

yoAlex5
  • 29,217
  • 8
  • 193
  • 205
  • In case of "obsolete" option, cannot use it. According to this, the option is not implemented. https://bugs.swift.org/browse/SR-8168 – mkjwa Feb 09 '20 at 03:41
0

Swift 5.0

Deprecate any method/class/struct/protocols using @available

@available(*, deprecated, message: "Parse your data by hand instead")
func parseData() { }

@available(*, deprecated, renamed: "loadData")
func fetchData() { }

@available(swift, obsoleted: 4.1, renamed: "attemptConnection")
func testConnection() { }

@available(swift, deprecated: 4.0, obsoleted: 5.0, message: "This will be removed in v5.0; please migrate to a different API.")

Possible params:

  • introduced
  • deprecated
  • obsoleted
  • message
  • renamed

For more info see apple doc: Attributes

Original Answer: https://stackoverflow.com/a/56379453/4061501

Lal Krishna
  • 15,485
  • 6
  • 64
  • 84
0

I wanted to deprecate the default init method from a specific version so I have done the following:

// Old Method to be deprecated
@available(*, unavailable, message: "This method has been deprecated from version 2.0.0. Please use new init method as init(param1:param2:)")
@objc public override init() {
    super.init()
    
    /**
     * This fatalError has been thrown if anyone is using this function in Objective-C and initializes this with the following syntax.
     * MyClass *classObj = [MyClass new];
     */
    fatalError("This method has been deprecated from version 2.0.0. Please use new init method as init(param1:param2:)")
}

// New init method
@objc public required init(param1: String, param2: String) {
    super.init()
    
    // New init method logic goes here.
}