__attribute__((deprecated))不适用于objective-c协议方法? [英] __attribute__ ((deprecated)) does not work on objective-c protocol methods?
问题描述
我需要在objective-c协议中弃用一个方法。在正常的类/实例方法上,我添加 __ attribute__((deprecated));
I need to deprecate a single method in objective-c protocol. On normal class/instance methods I add __attribute__ ((deprecated));
after declaration.
它不适用于协议方法。如果我标记他们已弃用,并在项目编译正确,没有预期的deprecation警告。
It seems, that it does not work on protocol methods. If I mark them deprecated and use them somewhere the project compiles OK, without expected deprecation warning.
这是一个缺陷在苹果LLVM 3.1,或者我做错了吗?
Is it a flaw in Apple LLVM 3.1, or am I doing something wrong?
推荐答案
虽然这里的答案提供了一些非常好的信息,但它们已经过时。从Xcode 5.0和LLVM 5.0开始,它看起来像Objective-C协议方法的deprecation警告被识别。当实现该方法时,Xcode 5标记它:
Although the answers here provide some very good information, they are outdated. Starting with Xcode 5.0 and LLVM 5.0 it looks like deprecation warnings for Objective-C protocol methods are recognized. When implementing the method, Xcode 5 flags it:
警告:实现已弃用的方法
Warning: Implementing deprecated method
这里是我用来产生一个废弃的协议方法实现的废弃警告的步骤:
Here are the steps I used to produce a deprecation warning for the implementation of a deprecated protocol method:
-
使用
__ deprecated
将协议方法标记为已弃用。从新的SDK 7.0文档:
Mark the protocol method as deprecated using
__deprecated
. From the new SDK 7.0 documentation:
__不推荐导致
编译器产生警告当遇到使用已弃用的功能的代码。 __ deprecated_msg()
也是这样,支持它的编译器将打印一条消息以及废弃警告。这可能需要使用 -Wdeprecated
标志打开此类警告。 __ deprecated_enum_msg()
应用于枚举,支持它的编译器将打印废弃警告。
__deprecated causes
the compiler to produce a warning when encountering code using the deprecated functionality.__deprecated_msg()
does the same, and compilers that support it will print a message along with the deprecation warning. This may require turning on such warning with the-Wdeprecated
flag.__deprecated_enum_msg()
should be used on enums, and compilers that support it will print the deprecation warning.
#define __deprecated __attribute__((deprecated))
若要弃用您的方法,请执行以下操作:
To deprecate your method, do something like this:
- (void)aDeprecatedProtocolMethod __deprecated;
这个单独的应该足以让Xcode显示一个弃用警告。
This alone should be enough for Xcode to display a deprecation warning. However, you should follow the next few steps (knowing that Xcode can be very finicky at times) to ensure the warning displays.
添加一个文档注释,其中包含一个文档注释弃用警告标记。请参阅下面的代码示例,了解如何操作:
Add a documentation comment with a deprecation warning tag. See the code example below to learn how:
/** Describe the method here - what does it do, how does it work, etc. Very brief.
@deprecated This delegate method is deprecated starting in version 2.0, please use otherMethodNameHere:withAnExtraParameter: instead. */
- (void)aDeprecatedProtocolMethod __deprecated;
清除项目(⌘ + + K ),然后构建项目(⌘ + B
Clean the project (⌘+⇧+K) and then Build the project (⌘+B) - just because Xcode can be funky sometimes.
我不能100%确定此功能的引入时间或地点(可能是SDK 7.0和10.9或Xcode 5.0 / 5.0 .1或LLVM 5.0) - 但它仍然有效。
I'm not 100% sure when or where this feature was introduced (maybe with SDK 7.0 and 10.9, or Xcode 5.0 / 5.0.1, or with LLVM 5.0) - but it works nonetheless.
这篇关于__attribute__((deprecated))不适用于objective-c协议方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!