Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I flag a method as deprecated in Objective-C 2.0?

People also ask

How do you make a method deprecated?

Using the @Deprecated Annotation To use it, you simply precede the class, method, or member declaration with "@Deprecated." Using the @Deprecated annotation to deprecate a class, method, or field ensures that all compilers will issue warnings when code uses that program element.

How do you mark a function as deprecated?

To mark a method as deprecated we can use the JavaDoc @deprecated tag. This is what we did since the beginning of Java. But when a new metadata support introduced to the Java language we can also use annotation. The annotation for marking method as deprecated is @Depreated .

How do you mark a class deprecated?

You need to use the [Obsolete] attribute. Example: [Obsolete("Not used any more", true)] public class MyDeprecatedClass { //... }

Is Objective C deprecated?

It won't be deprecated, but it'll move to Florida to enjoy its golden years. It'll spend days running the legacy app with a million lines of code, and its nights sipping margaritas with the OAuth library everyone fears rewriting.


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

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

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.


Use the deprecated attribute:

- (int)bar: (int)x __attribute__((deprecated));

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