代码之家  ›  专栏  ›  技术社区  ›  raistlin

我可以在Objective-C中为我自己的反对添加修正吗?

  •  2
  • raistlin  · 技术社区  · 7 年前

    想知道我是否可以为自己的反对意见创建自己的“修复”建议?有可能吗?如果是,那么任何资源将不胜感激!

    1 回复  |  直到 7 年前
        1
  •  6
  •   rob mayoff    7 年前

    你可以用 deprecated 属性:

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

    如果您想从特定OS版本开始弃用,可以使用 clang's availability attribute . 请注意,您只能根据操作系统版本而不是您自己库的版本来弃用。

    #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;
    }
    

    API_DEPRECATED_WITH_REPLACEMENT 中定义的宏 <os/availability.h> 而不是直接使用clang属性。头文件中有注释解释了它的用法。