How to deprecate a method in Xcode

前端 未结 5 2042
抹茶落季
抹茶落季 2020-12-07 12:08

We have our library we ship to our customers, and I\'d like to mark some methods as \"deprecated\" because we changed them (like Apple does in the iPhone SDK).

I\'ve

5条回答
  •  误落风尘
    2020-12-07 12:33

    __attribute__((deprecated)) is the gcc way (also supported in clang) of marking a function / method as deprecated. When one is marked as "deprecated", a warning will be produced whenever anyone calls it.

    The syntax for normal functions would be

    __attribute__((deprecated))
    void f(...) {
      ...
    }
    
    // gcc 4.5+ / clang
    __attribute__((deprecated("g has been deprecated please use g2 instead")))
    void g(...) {
      ...
    }
    

    and that of Objective-C methods would be

    @interface MyClass : NSObject { ... }
    -(void)f:(id)x __attribute__((deprecated));
    ...
    @end
    

    You can also mark the whole class as deprecated with

    __attribute__((deprecated))
    @interface DeprecatedClass : NSObject { ... }
    ...
    @end
    

    Apple also provides the header which provides the DEPRECATED_ATTRIBUTE and DEPRECATED_MSG_ATTRIBUTE(msg) macros that expand to the above attributes, or nothing if the compiler doesn't support attributes. Note that this header doesn't exist outside of OS X / iOS.


    Side note, if you are using Swift you use the @available attribute to deprecate an item, e.g.

    @available(*, deprecated=2.0, message="no longer needed")
    func f() {
        ...
    }
    

提交回复
热议问题