149

I'm part of a team developing a fairly large iPad app and there are many different classes we've created as a result. The trouble is some of the methods are now pretty much obsolete and I don't want simply remove them yet as I know some parts of the overall system use the methods... but there are better (newer) variants available which should be used instead (some of the old ones actually call the new ones, but the overall class interface is getting messy).

Is there a way in which I can mark certain methods as deprecated (like @deprecated in Java and [Obsolete] in .NET).

I see that Apple uses Availability.h and have tags such as

__OSX_AVAILABLE_BUT_DEPRECATED(__MAC_NA,__MAC_NA,__IPHONE_2_0,__IPHONE_3_0);

... is this the only way to do it (+ is it App Store safe to do this?) or are there alternatives which will flag a warning in XCode?

S. C.
  • 110
  • 1
  • 13
Jamie Chapman
  • 4,229
  • 5
  • 29
  • 47

5 Answers5

166

Deprecation Syntax

Syntax is provided to mark methods as deprecated:

@interface SomeClass
-method __attribute__((deprecated));
@end

or:

#include <AvailabilityMacros.h>
@interface SomeClass
-method DEPRECATED_ATTRIBUTE;  // or some other deployment-target-specific macro
@end
Shay Erlichmen
  • 31,691
  • 7
  • 68
  • 87
  • 7
    This macro makes sense to me, it sort of retains the feel of the `__attribute__` syntax. `#define __deprecated__ __attribute__((deprecated))` – zekel May 25 '11 at 15:03
  • Interestingly Xcode does not give me any warnings for using a method marked as depreciated. Is there a compiler flag that needs to be set? – memmons Sep 30 '11 at 18:07
  • In Xcode's code completion dropdown, I see that the method is marked as depreciated, but using it doesn't give a compiler warning. – memmons Sep 30 '11 at 18:27
  • 1
    @Answerbot Build settings > warn about deprecated functions ... set this to YES – bandejapaisa May 20 '12 at 08:54
  • how to add the Alternative method that should be used ? – OXXY Sep 28 '15 at 11:11
  • Probably it's not very important, but `DEPRECATED_ATTRIBUTE` is easier to notice when looking through the list of functions. – kelin Apr 28 '20 at 14:04
139

IMHO, it's easier to write __deprecated:

- (void)myDeprecatedMethod __deprecated;
- (int)methodNameDeprecated:(int)param __deprecated;

Works too on classes

__deprecated
@interface MyDeprecatedClass

  // ... some properties and methods ...

@end
Víctor B.
  • 1,630
  • 2
  • 14
  • 21
95

If you want to give additional message with the deprecation flag, you can use following flags.

@property (strong, nonatomic) NSString *catName
                    __deprecated_msg("use name instead.");

//  -- Or -- 
@property (strong, nonatomic) NSString *catName
                    DEPRECATED_MSG_ATTRIBUTE("use name instead.");

//  -- Or -- 
@property (strong, nonatomic) NSString *catName
                    __attribute__((deprecated("use name instead.")));

Using above mentioned flags, you can tell why you are deprecating or what is the method developer should use in future.

uiroshan
  • 5,021
  • 2
  • 39
  • 37
  • 3
    I prefer to have a message with a deprecation warning. It’s much more helpful to new users of an API. So, I think this is the best answer. – johnnieb Dec 20 '14 at 19:12
  • I like this answer best, being the clearest and easiest to use with copy-paste of just what I need. Could you please also enhance it with sample of deprecating a method? a whole class? Is it done exactly the same way? – Motti Shneor Nov 06 '19 at 08:36
18

To mark a method as deprecated, use __attribute__((deprecated("Your message goes here")))

A practical example, from Mantle

@interface NSValueTransformer (UnavailableMTLPredefinedTransformerAdditions)

+ (NSValueTransformer *)mtl_externalRepresentationTransformerWithModelClass:(Class)modelClass __attribute__((deprecated("Replaced by +mtl_JSONDictionaryTransformerWithModelClass:")));
+ (NSValueTransformer *)mtl_externalRepresentationArrayTransformerWithModelClass:(Class)modelClass __attribute__((deprecated("Replaced by +mtl_JSONArrayTransformerWithModelClass:")));

@end
onmyway133
  • 45,645
  • 31
  • 257
  • 263
16

Use the deprecated attribute:

- (int)bar: (int)x __attribute__((deprecated));
Stephen Canon
  • 103,815
  • 19
  • 183
  • 269