raistlin
raistlin

Reputation: 4346

Can I add fix-it for my own deprecations in Objective-C?

Trying to find if I can create my own "fix it" suggestion for my own deprecations? Is it possible at all? If yes, then any resource would be much appreciated!

Upvotes: 2

Views: 362

Answers (1)

rob mayoff
rob mayoff

Reputation: 385700

You can use the deprecated attribute:

@interface MyObject: NSObject
- (void)oldMethod
    __attribute__((deprecated("Don't use this", "newMethod")))
    ;
- (void)newMethod;
@end

If you want to deprecate as of a specific OS version, you can use clang's availability attribute. Note that you can only deprecate based on OS version, not on your own library's version.

Example:

#import <Foundation/Foundation.h>

@interface MyObject: NSObject
- (void)oldMethod
    __attribute__((availability(ios,deprecated=12.0,replacement="newMethod")))
    ;
- (void)newMethod;
@end

@implementation MyObject

- (void)oldMethod { }
- (void)newMethod { }

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        MyObject *o = [[MyObject alloc] init];
        [o oldMethod]; // Xcode offers a fix-it to use newMethod instead.
    }
    return 0;
}

You can use the API_DEPRECATED_WITH_REPLACEMENT macro defined in <os/availability.h> instead of directly using the clang attribute, if you want. There are comments in that header file explaining its use.

Upvotes: 6

Related Questions