Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deprecation and other attributes of methods in Swift, how?

Tags:

ios

swift

iphone

In Objective-C I can do this

- (id)init __attribute__((unavailable("init is unavailable, use initWithFrame"))); 

to warn users that should not use that method for initialization of a class and I can add this other __attribute to deprecate a method

+(void)shareWithParams:(NSDictionary *)params __attribute((deprecated("use shareWithPars: instead")));  

Is that possible to do something like that in Swift?

like image 968
Duck Avatar asked Jul 10 '15 16:07

Duck


People also ask

What happens when a method is deprecated?

Similarly, when a class or method is deprecated, it means that the class or method is no longer considered important. It is so unimportant, in fact, that it should no longer be used at all, as it might well cease to exist in the future. The need for deprecation comes about because as a class evolves, its API changes.

What are Swift attributes?

There are two kinds of attributes in Swift—those that apply to declarations and those that apply to types. An attribute provides additional information about the declaration or type.

What does deprecated mean in Swift?

Deprecated methods or classes that are outdated one which will eventually be removed.

What is @frozen struct in Swift?

Frozen Structs. To opt out of this flexibility, a struct may be marked @frozen . This promises that no stored properties will be added to or removed from the struct, even non-ABI-public ones, and allows the compiler to optimize as such. These stored properties also must not have any observing accessors.


1 Answers

Swift has an available attribute that you can use for this. It's available arguments include

  • introduced
  • deprecated
  • obsoleted
  • message
  • renamed.

Or for the example you gave:

@available(*, unavailable, message: "init is unavailable, use initWithFrame") init() {  }  @available(*, deprecated, message: "use shareWithPars: instead") class func shareWithParams(params: NSDictionary) {  } 

For more information on these attributes, check out the Attributes section in The Swift Programming Language. (currently page 627)

like image 132
Mick MacCallum Avatar answered Oct 07 '22 21:10

Mick MacCallum