如何在 iOS Objective-C 头文件中将函数标记为已弃用?

发布于 2024-08-04 16:24:42 字数 134 浏览 6 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

戈亓 2024-08-11 16:24:42

尝试在方法声明中附加一个属性:

- (void)fooBar __attribute__ ((deprecated));

取自此处

Try appending an attribute to your method declaration:

- (void)fooBar __attribute__ ((deprecated));

Taken from here.

呆° 2024-08-11 16:24:42

您可以使用 中定义的宏来代替 __attribute__((deprecated))

- (void)fooBar __deprecated;
// Or better:
- (void)fooBar __deprecated_msg("Use barFoo instead.");

或者您可以使用 < 中定义的宏;AvailabilityMacros.h>

- (void)fooBar DEPRECATED_ATTRIBUTE;
// Or better:
- (void)fooBar DEPRECATED_MSG_ATTRIBUTE("Use barFoo instead.");

如果您使用 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>:

- (void)fooBar __deprecated;
// Or better:
- (void)fooBar __deprecated_msg("Use barFoo instead.");

Or you can use the macros defined in <AvailabilityMacros.h>:

- (void)fooBar DEPRECATED_ATTRIBUTE;
// Or better:
- (void)fooBar DEPRECATED_MSG_ATTRIBUTE("Use barFoo instead.");

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, then DEPRECATED_MSG_ATTRIBUTE() uses the optimal availability definitions (for instance, it supports GCC3.1).

蓝色星空 2024-08-11 16:24:42

蒂姆的回答实际上会产生编译器警告;其他版本只是注释,对编译器没有任何影响。

如果您查看 /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.

む无字情书 2024-08-11 16:24:42

来自 Apple 的 SFAuthorization.h:

/*!
DEPRECATED: Use obtainWithRight:flags:error:
@method permitWithRight:flags:
@abstract Call permitWithRight to gain a right to have
          access to a privilege operation.
@param rightName The name of an authorization right.
@param flags Authorization flags.
*/
- (OSStatus)permitWithRight:(AuthorizationString)rightName
                      flags:(AuthorizationFlags)flags;

如果您没有使用自动文档生成器,我会说这样就足够了:

- (void)doSomething;           /* DEPRECATED */

From Apple's SFAuthorization.h:

/*!
DEPRECATED: Use obtainWithRight:flags:error:
@method permitWithRight:flags:
@abstract Call permitWithRight to gain a right to have
          access to a privilege operation.
@param rightName The name of an authorization right.
@param flags Authorization flags.
*/
- (OSStatus)permitWithRight:(AuthorizationString)rightName
                      flags:(AuthorizationFlags)flags;

If you're not using an automated documentation builder I'd say something like this is enough:

- (void)doSomething;           /* DEPRECATED */
指尖凝香 2024-08-11 16:24:42

您还可以按照 HeaderDoc 手册。使用此语法的地方:

/*!
 * @abstract Foo is good for bar.
 *
 * @deprecated in version 2.0
 */

You could also follow the HeaderDoc manual. Where this syntax is used:

/*!
 * @abstract Foo is good for bar.
 *
 * @deprecated in version 2.0
 */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文