如何弃用 Objective-C 中的方法
我们有我们的库交付给客户,我想将一些方法标记为“已弃用”,因为我们更改了它们(就像 Apple 在 iPhone SDK 中所做的那样)。
我看到了 __OSX_AVAILABLE_BUT_DEPRECATED
预处理器宏,它映射到 __AVAILABILITY_INTERNAL
,它又映射到 __attribute__((deprecated))
。 嗯,
我对这个东西有点困惑!
有人知道这件事吗?
We have our library we ship to our customers, and I'd like to mark some methods as "deprecated" because we changed them (like Apple does in the iPhone SDK).
I've seen the __OSX_AVAILABLE_BUT_DEPRECATED
pre-processor macro, which is mapped to __AVAILABILITY_INTERNAL
, which is mapped to __attribute__((deprecated))
...
Well i'm a bit confused with this stuff!
Anybody knows something about that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
__attribute__((已弃用))
是普通函数的语法为
,Objective-C 方法的语法为
您还可以将整个类标记为已弃用,
Apple 还提供了
标头,该标头提供了 DEPRECATED_ATTRIBUTE 和 DEPRECATED_MSG_ATTRIBUTE (msg) 扩展为上述属性的宏,或者如果编译器不支持属性则什么也不包含。请注意,此标头在 OS X / iOS 之外不存在。旁注,如果您使用 Swift,则使用
@available
属性 弃用某个项目,例如__attribute__((deprecated))
is the gcc way (also supported in clang) of marking a function / method as deprecated. When one is marked as "deprecated", a warning will be produced whenever anyone calls it.The syntax for normal functions would be
and that of Objective-C methods would be
You can also mark the whole class as deprecated with
Apple also provides the
<AvailabilityMacros.h>
header which provides the DEPRECATED_ATTRIBUTE and DEPRECATED_MSG_ATTRIBUTE(msg) macros that expand to the above attributes, or nothing if the compiler doesn't support attributes. Note that this header doesn't exist outside of OS X / iOS.Side note, if you are using Swift you use the
@available
attribute to deprecate an item, e.g.您还可以使用更具可读性的 define
DEPRECATED_ATTRIBUTE
它在
usr/include/AvailabilityMacros.h
中定义:Objective-C方法示例:
您还可以将整个类标记为已弃用:
You can also use more readable define
DEPRECATED_ATTRIBUTE
It defined in
usr/include/AvailabilityMacros.h
:Objective-C methods example:
You can also mark the whole class as deprecated:
Swift 5.0
使用
@available
弃用任何方法/类/结构/协议可能的参数:
有关详细信息,请参阅 Apple 文档:属性
Swift 5.0
Deprecate any method/class/struct/protocols using
@available
Possible params:
For more info see apple doc: Attributes
如果您在 xcode 项目中使用 C++14,您还可以使用
[[deprecated]]
或[[deprecated("reason")]]
属性现在是语言的一部分。请参阅文档: http://en.cppreference.com/w/cpp/language/attributes< /a>
If you are using C++14 in your xcode project, you may also use the
[[deprecated]]
or[[deprecated("reason")]]
attribute that is now part of the language.see documentation: http://en.cppreference.com/w/cpp/language/attributes
- 对于 SWIFT 代码:
将其放在方法的正上方:
@available(*,已弃用:<#Version#>,消息:<#Message#>)
示例:
- FOR SWIFT CODE:
Put this right above the method:
@available(*, deprecated: <#Version#>, message: <#Message#>)
example: