繁体   English   中英

如何弃用Xcode中的方法

[英]How to deprecate a method in Xcode

我们将我们的库提供给客户,我想将一些方法标记为“已弃用”,因为我们更改了它们(就像Apple在iPhone SDK中所做的那样)。

我见过__OSX_AVAILABLE_BUT_DEPRECATED预处理器宏,映射到__AVAILABILITY_INTERNAL ,映射到__attribute__((deprecated)) ...

好吧,我对这些东西有点困惑!

有谁知道这件事?

__attribute__((deprecated))是将函数/方法标记为已弃用的gcc方式在clang中支持 )。 当一个被标记为“已弃用”时,每当有人调用它时都会产生警告。

正常函数的语法是

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

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

而Objective-C方法则是

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

您还可以将整个类标记为已弃用

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

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


注意,如果您使用Swift,则使用@available属性来弃用项目,例如

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

您还可以使用更易读的定义 DEPRECATED_ATTRIBUTE

它在usr/include/AvailabilityMacros.h定义:

#define DEPRECATED_ATTRIBUTE        __attribute__((deprecated))
#define DEPRECATED_MSG_ATTRIBUTE(msg) __attribute((deprecated((msg))))

Objective-C方法示例:

@interface MyClass : NSObject { ... }
-(void)foo:(id)x DEPRECATED_ATTRIBUTE;

// If you want to specify deprecated message:
-(void)bar:(id)x DEPRECATED_MSG_ATTRIBUTE("Use baz: method instead.");
...
@end

您还可以将整个类标记为已弃用:

DEPRECATED_ATTRIBUTE
@interface DeprecatedClass : NSObject { ... }
...
@end

如果您在xcode项目中使用C ++ 14,则还可以使用[[deprecated]][[deprecated("reason")]]属性,该属性现在是语言的一部分。

请参阅文档: http//en.cppreference.com/w/cpp/language/attributes

- 对于SWIFT代码:

把它放在方法的上方: @available(*, deprecated: <#Version#>, message: <#Message#>)

例:

@available(*, deprecated: 11, message: "Use color assets instead")
public struct ColorPaletteItemResource: ColorPaletteItemResourceType {
    ...
}

Swift 5.0

使用@available弃用任何方法/类/结构/协议

@available(*, deprecated, message: "Parse your data by hand instead")
func parseData() { }

@available(*, deprecated, renamed: "loadData")
func fetchData() { }

@available(swift, obsoleted: 4.1, renamed: "attemptConnection")
func testConnection() { }

@available(swift, deprecated: 4.0, obsoleted: 5.0, message: "This will be removed in v5.0; please migrate to a different API.")

可能的参数:

  • 介绍
  • 弃用
  • 废弃
  • 信息
  • 改名

有关更多信息,请参阅apple doc: Attributes

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM