ios 如何手动弃用成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25405133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to manually deprecate members
提问by Atomix
Unlike Objective-C, Swift has no preprocessor, so is there still a way to manually deprecate members of a class?
与 Objective-C 不同,Swift 没有预处理器,那么还有办法手动弃用类的成员吗?
I am looking for something similar to this:
我正在寻找类似的东西:
-(id)method __deprecated;
回答by Axel Guilmin
You can use the Available tag, for example :
您可以使用可用标签,例如:
@available(*, deprecated)
func myFunc() {
// ...
}
Where * is the platform (iOS, iOSApplicationExtension, macOS, watchOS, tvOS, * for all, etc.).
其中 * 是平台(iOS、iOSApplicationExtension、macOS、watchOS、tvOS、* for all 等)。
You can also specify the version of the platform from which it was introduced
, deprecated
, obsoleted
, renamed
, and a message
:
您还可以指定的平台,它的版本introduced
,deprecated
,obsoleted
,renamed
,和message
:
@available(iOS, deprecated:6.0)
func myFunc() {
// calling this function is deprecated on iOS6+
}
Or
@available(iOS, deprecated: 6.0, obsoleted: 7.0, message: "Because !")
func myFunc() {
// deprecated from iOS6, and obsoleted after iOS7, the message "Because !" is displayed in XCode warnings
}
If your project targets multiple platforms, you can use several tags like so :
如果您的项目面向多个平台,您可以使用多个标签,如下所示:
@available(tvOS, deprecated:9.0.1)
@available(iOS, deprecated:9.1)
@available(macOS, unavailable, message: "Unavailable on macOS")
func myFunc() {
// ...
}
More details in the Swift documentation.
Swift 文档中的更多详细信息。
回答by Yuchen Zhong
Starting Swift 3and Swift 4, the version number is optional. You can now simply type:
从Swift 3和Swift 4 开始,版本号是可选的。您现在可以简单地输入:
@available(*, deprecated)
func foo() {
// ...
}
Or if you want a message go along with it:
或者,如果您想要一条消息,请执行以下操作:
@available(*, deprecated, message: "no longer available ...")
func foo() {
// ...
}
回答by Skyborg
You can use this to auto-fix you entrys with your new func
您可以使用它来自动修复您的新功能条目
@available(*, deprecated, renamed: "myNewFunc")
func myOldFunc() {
// ...
}
func myNewFunc() {
// ...
}
Instead of * you can use swift , for the swift Version number.
您可以使用 swift 代替 * 来获取 swift 版本号。
Deprecated functions generate warnings but can still be called. (Warning)
不推荐使用的函数会生成警告,但仍然可以调用。(警告)
Obsolete functions stop it from being called entirely. (Error)
过时的函数阻止它被完全调用。(错误)
@available(swift, deprecated: 4.0, obsoleted: 4.2, message: "This will be removed in v4.2, please migrate to ...")
or use other Options like iOS, macOS, watchOS, tvOS ...
或使用其他选项,如 iOS、macOS、watchOS、tvOS ...
回答by yoAlex5
@available(iOS, deprecated:7.0, obsoleted: <ObsoletedVersion>, renamed: "myFuncNew", message: "Please use new method - myFuncNew()")
func myFuncOld() {
//
}
If deployment target
is 9.0
and
如果deployment target
是9.0
并且
1.<ObsoletedVersion>
== 10.0
- warning
1. <ObsoletedVersion>
== 10.0
-warning
2.<ObsoletedVersion>
== 8.0
- compile error
2. <ObsoletedVersion>
== 8.0
-compile error