Swift中方法的弃用和其他属性如何? [英] Deprecation and other attributes of methods in Swift, how?

查看:64
本文介绍了Swift中方法的弃用和其他属性如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-C中,我可以做到

In Objective-C I can do this

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

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

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"))); 

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

Is that possible to do something like that in Swift?

推荐答案

Swift具有一个 available 属性,您可以将其用于此目的.可用的参数包括

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) {

}

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

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

这篇关于Swift中方法的弃用和其他属性如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆