我可以在 Objective-C 中为我自己的弃用添加修复吗?
Can I add fix-it for my own deprecations in Objective-C?
想看看我是否可以为自己的弃用创建自己的 "fix it" 建议?有可能吗?如果是,那么任何资源将不胜感激!
您可以使用 deprecated
属性:
@interface MyObject: NSObject
- (void)oldMethod
__attribute__((deprecated("Don't use this", "newMethod")))
;
- (void)newMethod;
@end
如果您想从特定 OS 版本开始弃用,可以使用 clang's availability
attribute。请注意,您只能根据 OS 版本弃用,不能根据您自己的库版本弃用。
示例:
#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;
}
如果需要,您可以使用 <os/availability.h>
中定义的 API_DEPRECATED_WITH_REPLACEMENT
宏,而不是直接使用 clang 属性。该头文件中有解释其用途的注释。
想看看我是否可以为自己的弃用创建自己的 "fix it" 建议?有可能吗?如果是,那么任何资源将不胜感激!
您可以使用 deprecated
属性:
@interface MyObject: NSObject
- (void)oldMethod
__attribute__((deprecated("Don't use this", "newMethod")))
;
- (void)newMethod;
@end
如果您想从特定 OS 版本开始弃用,可以使用 clang's availability
attribute。请注意,您只能根据 OS 版本弃用,不能根据您自己的库版本弃用。
示例:
#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;
}
如果需要,您可以使用 <os/availability.h>
中定义的 API_DEPRECATED_WITH_REPLACEMENT
宏,而不是直接使用 clang 属性。该头文件中有解释其用途的注释。