Swift中的方法的弃用和其他属性,如何?

Spa*_*Dog 35 iphone ios swift

在Objective-C中,我可以做到这一点

- (id)init __attribute__((unavailable("init is unavailable, use initWithFrame")));
Run Code Online (Sandbox Code Playgroud)

警告不应该使用该方法初始化类的用户,我可以添加其他__attribute来弃用方法

+(void)shareWithParams:(NSDictionary *)params
__attribute((deprecated("use shareWithPars: instead"))); 
Run Code Online (Sandbox Code Playgroud)

是否有可能在Swift中做类似的事情?

Mic*_*lum 70

Swift有一个available可用于此的属性.它的可用参数包括

  • 介绍
  • 弃用
  • 废弃
  • 信息
  • 重命名.

或者您给出的示例:

@available(*, unavailable, message: "init is unavailable, use initWithFrame")
init() {

}

@available(*, deprecated, message: "use shareWithPars: instead")
class func shareWithParams(params: NSDictionary) {

}
Run Code Online (Sandbox Code Playgroud)

有关这些属性的更多信息,请查看Swift编程语言中的属性部分.(当前第627页)


pab*_*ros 9

对于Swift 3和Swift 4 =,您必须使用:符号,而不是使用符号来设置消息.例如:

@available(*, deprecated, message: "Use EndPointModel class instead")
class BaseModel {

}
Run Code Online (Sandbox Code Playgroud)