Deprecation and other attributes of methods in Swift, how?

眉间皱痕 提交于 2021-02-17 09:13:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!