xcode __attribute__ ((deprecated)) 对objective-c 协议方法不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11432452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
__attribute__ ((deprecated)) does not work on objective-c protocol methods?
提问by Vilém Kurz
I need to deprecate a single method in objective-c protocol. On normal class/instance methods I add __attribute__ ((deprecated));
after declaration.
我需要弃用 Objective-c 协议中的一个方法。在普通的类/实例方法上,我__attribute__ ((deprecated));
在声明后添加。
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.
看来,它不适用于协议方法。如果我将它们标记为已弃用并在某处使用它们,则项目编译正常,而不会出现预期的弃用警告。
Is it a flaw in Apple LLVM 3.1, or am I doing something wrong?
这是 Apple LLVM 3.1 的缺陷,还是我做错了什么?
采纳答案by Sam Spencer
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:
虽然这里的答案提供了一些非常好的信息,但它们已经过时了。从 Xcode 5.0 和 LLVM 5.0 开始,似乎可以识别 Objective-C 协议方法的弃用警告。在实现该方法时,Xcode 5 将其标记为:
Warning: Implementing deprecated method
警告:实施已弃用的方法
Here are the steps I used to produce a deprecation warning for the implementation of a deprecated protocol method:
以下是我用于为已弃用协议方法的实现生成弃用警告的步骤:
Mark the protocol method as deprecated using
__deprecated
. From the new SDK 7.0 documentation:__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;
This alone shouldbe 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;
Clean the project (?+?+K) and then Build the project (?+B) - just because Xcode can be funky sometimes.
使用 将协议方法标记为已弃用
__deprecated
。来自新的 SDK 7.0 文档:__deprecated causes
编译器在遇到使用已弃用功能的代码时发出警告。__deprecated_msg()
做同样的事情,支持它的编译器将打印一条消息以及弃用警告。这可能需要使用-Wdeprecated
标志打开此类警告。__deprecated_enum_msg()
应该用于枚举,支持它的编译器将打印弃用警告。#define __deprecated __attribute__((deprecated))
要弃用您的方法,请执行以下操作:
- (void)aDeprecatedProtocolMethod __deprecated;
仅此一点就应该是足够的Xcode显示弃用警告。但是,您应该遵循接下来的几个步骤(知道 Xcode 有时可能非常挑剔)以确保显示警告。
添加带有弃用警告标签的文档注释。请参阅下面的代码示例以了解如何:
/** 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) - 只是因为 Xcode 有时会很时髦。
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.
我不能 100% 确定何时何地引入了此功能(可能是 SDK 7.0 和 10.9,或 Xcode 5.0 / 5.0.1,或 LLVM 5.0) - 但它仍然有效。
回答by Vilém Kurz
Well, I just realised, that even Apple use __attribute__((deprecated))
at the end. And it does not work either. If I use any deprecated delegate method, e.g.
好吧,我刚刚意识到,即使是 Apple__attribute__((deprecated))
最终也会使用。它也不起作用。如果我使用任何已弃用的委托方法,例如
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView
accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
there is no warning.
没有警告。
So it seems like a candidate for radar.
所以它似乎是雷达的候选者。
EDIT: filed a radar, Bug ID# 11849771.
编辑:提交雷达,错误 ID# 11849771。
回答by Wolfgang Schreurs
Apple deprecated some methods in the UITableViewDelegate protocol, perhaps you'll be able to find the solution using Apple's code as example.
Apple 弃用了 UITableViewDelegate 协议中的一些方法,也许您可以使用 Apple 的代码作为示例找到解决方案。
The relevant code of the protocol is as follows:
协议相关代码如下:
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView
accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
__OSX_AVAILABLE_BUT_DEPRECATED(__MAC_NA,__MAC_NA,__IPHONE_2_0,__IPHONE_3_0);
As you can see, Apple uses a macro. Perhaps this is the way to go?
如您所见,Apple 使用了宏。也许这是要走的路?
EDIT:As noted on the following link [1] __attribute__((deprecated))
is a GCC construct so this might not work in LLVM. I guess this is the reason Apple uses macros, so some other (or no) deprecation construct will be called when other compilers are used.
编辑:如以下链接所述 [1]__attribute__((deprecated))
是 GCC 构造,因此这可能不适用于 LLVM。我想这就是 Apple 使用宏的原因,因此在使用其他编译器时将调用其他(或不使用)弃用构造。