To mark some of your own API as deprecated in Objective-C, you simply can add the following attribute to your method declaration (as seen in the snippet below):
-(void)myOldMethod __attribute((deprecated("Use the myNewMethod instead."))); |
Now when you call myOldMethod in your code, the compiler will warn you that: “myOldMethod is deprecated. Use the myNewMethod instead”. You can also add the deprecated attribute to property declarations.
But what if you wanted to be more specific? For instance, if there is a better way to handle something in the iOS 8 SDK, you can mark an old method as deprecated only for projects that have iOS 8.0 or later as the deployment target.
-(void)myOldMethod NS_DEPRECATED_IOS(3_0, 8_0,"Use myNewMethod instead."); |
Using the code snippet above, if you call myOldMethod the compiler will only warn you that the method is deprecated if your project’s deployment target is set to iOS 8 or later.
Deprecating your own methods can come in handy especially if you have written your own framework that is used across multiple projects. You may choose to deprecate some of your own API rather than removing the methods entirely until you get around to modifying your existing projects to use your new API.
Instead of marking each method as deprecated, is there a way to mark the whole class as deprecated?
Yes, you can mark an entire class as deprecated.
In NSObjCRuntime.h you can find some macros to do so.
As an example, the header file for a deprecated class could look like this:
——————————————————————————————————–
NS_CLASS_DEPRECATED_IOS(2_0, 4_0, “This has been replaced by the NewClass.”)
@interface OldClass : NSObject
@property (nonatomic) BOOL someProperty;
@end