如何在 iOS Objective-C 头文件中将函数标记为已弃用?
我猜只有一些关键字可以在函数之后粘贴在某个地方?
如果有人尝试使用已弃用的函数,我希望生成编译器警告,类似于 Apple API 中看到的行为。
如何在 iOS Objective-C 头文件中将函数标记为已弃用?
我猜只有一些关键字可以在函数之后粘贴在某个地方?
如果有人尝试使用已弃用的函数,我希望生成编译器警告,类似于 Apple API 中看到的行为。
__attribute__((deprecated))
您可以使用以下定义的宏来代替<cdefs.h>
:
- (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 short syntax __deprecated_msg()
。但是如果你使用 C 进行跨平台,那么DEPRECATED_MSG_ATTRIBUTE()
使用最优可用性定义(例如,它支持 GCC3.1)。
Tim 的回答实际上会产生编译器警告;其他版本只是对编译器没有影响的注释。
如果您查看 /usr/include/AvailabilityMacros.h,您会看到 Apple 是如何做到这一点的。该标头使用__attribute__((deprecated))
并__attribute__((unavailable))
取决于 API 是否存在但已弃用,或者实际上已从操作系统中删除。
来自 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 */
您也可以遵循HeaderDoc 手册。使用此语法的地方:
/*!
* @abstract Foo is good for bar.
*
* @deprecated in version 2.0
*/