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?
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.
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.
Deprecated methods or classes that are outdated one which will eventually be removed.
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.
Swift has an available
attribute that you can use for this. It's available arguments include
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With