如何在 iOS Objective-C 头文件中将函数标记为已弃用?
如何在 iOS Objective-C 头文件中将函数标记为已弃用?
我猜我可以在函数后面的某个地方粘贴一些关键字?
我希望如果任何人尝试使用已弃用的函数,都会生成编译器警告,类似于 Apple 的 API 中看到的行为。
How do I flag a function as being deprecated in an iOS Objective-C header file?
I'm guessing there's just some keyword I can stick after the function somewhere?
I would like for a compiler warning to be generated should anyone try and use the deprecated function, similar to the behavior seen in Apple's APIs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试在方法声明中附加一个属性:
取自此处 。
Try appending an attribute to your method declaration:
Taken from here.
您可以使用
中定义的宏来代替__attribute__((deprecated))
:或者您可以使用
< 中定义的宏;AvailabilityMacros.h>
:如果您使用 Objective-C,则没有什么区别,因为您将使用现代编译器,因此您可以使用 Apple 短语法
__deprecated_msg()
。但如果您使用 C 实现跨平台,则DEPRECATED_MSG_ATTRIBUTE()
使用最佳可用性定义(例如,它支持 GCC3.1)。Instead of
__attribute__((deprecated))
, you can use use the macros defined in<cdefs.h>
:Or you can use the macros defined in
<AvailabilityMacros.h>
:If you use Objective-C, it makes no difference as you are going to use a modern compiler, so you can go for Apple short syntax
__deprecated_msg()
. But if you use C for cross-platform, thenDEPRECATED_MSG_ATTRIBUTE()
uses the optimal availability definitions (for instance, it supports GCC3.1).蒂姆的回答实际上会产生编译器警告;其他版本只是注释,对编译器没有任何影响。
如果您查看 /usr/include/AvailabilityMacros.h,您将了解 Apple 是如何做到这一点的。该标头使用
__attribute__((deprecated))
和__attribute__((unavailable))
,具体取决于 API 是否存在但已弃用,或者实际上已从操作系统中删除。Tim's answer will actually produce a compiler warning; the other versions are merely comments which have no effect w.r.t. the compiler.
If you look at /usr/include/AvailabilityMacros.h, you'll see how Apple does this. That header uses
__attribute__((deprecated))
and__attribute__((unavailable))
depending on whether the API is present but deprecated, or has actually been removed from the OS.来自 Apple 的 SFAuthorization.h:
如果您没有使用自动文档生成器,我会说这样就足够了:
From Apple's SFAuthorization.h:
If you're not using an automated documentation builder I'd say something like this is enough:
您还可以按照 HeaderDoc 手册。使用此语法的地方:
You could also follow the HeaderDoc manual. Where this syntax is used: