Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__attribute__ ((deprecated)) does not work on objective-c protocol methods?

I need to deprecate a single method in objective-c protocol. On normal class/instance methods I add __attribute__ ((deprecated)); after declaration.

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?

like image 337
Vilém Kurz Avatar asked Jul 11 '12 12:07

Vilém Kurz


1 Answers

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:

  1. 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 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.

  2. 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;
    
  3. Clean the project (++K) and then Build the project (+B) - just because Xcode can be funky sometimes.

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.

like image 139
Sam Spencer Avatar answered Oct 14 '22 13:10

Sam Spencer