Deprecating methods in Objective-C

Nothing lasts forever

2018

Sometimes you think of a better way of doing things or you got something wrong and you need to deprecate the older version. It would be great if the compiler could help. Well it can!

The ever useful NSObjCRuntime.h defines:

#define NS_AVAILABLE(_mac, _ios)
#define NS_AVAILABLE_MAC(_mac)
#define NS_AVAILABLE_IOS(_ios)

#define NS_DEPRECATED(_macIntro, _macDep, _iosIntro, _iosDep, ...)
#define NS_DEPRECATED_MAC(_macIntro, _macDep, ...)
#define NS_DEPRECATED_IOS(_iosIntro, _iosDep, ...)

All of which are macros that expand out to __attributes__ which will help the compiler inform you and anyone else of their use. For example:

@interface SomeClass : NSObject
// ...
- (void)someDeprecatedMethod:(NSUInteger)someArg NS_DEPRECATED(10_4, 10_14, 2_0, 12_0);
// ...
@end

Where the method someDeprecatedMethod: was introduced in macOS 10.4 Tiger and iOS 2, and deprecated in macOS 10_14 Mojave and iOS 12.

Licence

Any source in this article is released under the ISC License.