How to manually ignore members

Unlike Objective-C, Swift does not have a preprocessor, so is there another way to manually condemn class members?

I am looking for something similar to this:

-(id)method __deprecated; 
+146
c-preprocessor ios deprecated objective-c swift
Aug 20 '14 at
source share
4 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 with which it was introduced , deprecated , obsoleted , renamed and 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 is focused on several platforms, you can use several tags, for example:

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

See the Swift documentation for more details.

+236
Aug 20 '14 at 13:40
source share

Running Swift 3 and Swift 4 , version number is optional. Now you can simply enter:

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

Or, if you want the message to continue:

 @available(*, deprecated, message: "no longer available ...") func foo() { // ... } 
+64
Oct 06 '16 at 20:50
source share

You can use this to automatically correct your notes with your new feature.

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

Deprecated functions prevent it from being called completely. (Mistake)

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

+3
Oct 16 '18 at 6:37
source share
 @available(iOS, deprecated:7.0, obsoleted: <ObsoletedVersion>, renamed: "myFuncNew", message: "Please use new method - myFuncNew()") func myFuncOld() { // } 

If deployment target 9.0 and

1. <ObsoletedVersion> == 10.0 - warning

enter image description here

2. <ObsoletedVersion> == 8.0 - compile error

enter image description here

0
Jul 04 '19 at 19:38
source share



All Articles