如何在Xcode中弃用一个方法 [英] How to deprecate a method in Xcode

查看:223
本文介绍了如何在Xcode中弃用一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有我们的库,我们发送给我们的客户,我想标记一些方法为弃用,因为我们改变它们(像苹果在iPhone SDK)。

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).

我看到了 __ OSX_AVAILABLE_BUT_DEPRECATED 预处理器宏,映射到 __ AVAILABILITY_INTERNAL ,这是映射到 __属性__((已弃用)) ...

I've seen the __OSX_AVAILABLE_BUT_DEPRECATED pre-processor macro, which is mapped to __AVAILABILITY_INTERNAL, which is mapped to __attribute__((deprecated))...

任何人都知道这一点?

推荐答案

__ attribute __((deprecated)) gcc way (也支持在clang )将一个函数/方法标记为已弃用。当一个人被标记为已弃用时,每当任何人调用它时都会产生警告。

__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.

正常函数的语法为

__attribute__((deprecated))
void f(...) {
  ...
}

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

,而Objective-C方法的结果是

and that of Objective-C methods would be

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

已弃用

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






还提供了扩展到上述属性的DEPRECATED_ATTRIBUTE和DEPRECATED_MSG_ATTRIBUTE(msg)宏的< AvailabilityMacros.h> 头,如果编译器不支持属性。请注意,此标题不存在于OS X / iOS之外。


Apple also provides the <AvailabilityMacros.h> 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.

注意,如果使用Swift请使用 @available 属性以废弃项目,例如

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() {
    ...
}

这篇关于如何在Xcode中弃用一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆