问题
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?
回答1:
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)
回答2:
For Swift 3 and Swift 4, instead of using the =
sign to set the message, you have to use the :
sign. For example:
@available(*, deprecated, message: "Use EndPointModel class instead")
class BaseModel {
}
来源:https://stackoverflow.com/questions/31346164/deprecation-and-other-attributes-of-methods-in-swift-how