How to properly deprecate a method in Xcode 4

2019-04-07 22:11发布

问题:

I've just looked over the answers here but didn't help. When I add a deprecated attribute only to method declaration than the compiler says Attributes on method implementation and its declaration must match. Do I need to add smth to method implementation?

Thanks!

回答1:

Just add the attribute to the declaration:

@interface Blah
- (void)method __attribute__((deprecated));
@end

Provided your includes are correct for the translation, this should work fine. Perhaps you have added the attribute to the definition, not the declaration? Otherwise, a demonstration (code sample) would help.

Update

Although the above approach works for typical messages, it appears clang gets confused with IBActions.

With clang, ibaction attributes are implicitly inserted (for what was formerly a typedef).

When the attribute is specified in the declaration only, the preprocessor output is as follows:

// preprocessed declaration
- (void)__attribute__((ibaction))setSomething:(id)sender __attribute__((noreturn));
// preprocessed implementation
- (void)__attribute__((ibaction))setSomething:(id)sender
...    

So, it appears the compiler is just confused by this hidden decoration, and you must also add the attribute to the implementation/definition to suppress the warning when the method is an IBAction.



回答2:

You have to put the deprecated attribute on both the declaration and the implementation of the method, at least in Xcode 4.3.2 with clang.