如何弃用 Objective-C 中的方法

发布于 2024-10-16 09:43:50 字数 270 浏览 8 评论 0原文

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

我看到了 __OSX_AVAILABLE_BUT_DEPRECATED 预处理器宏,它映射到 __AVAILABILITY_INTERNAL,它又映射到 __attribute__((deprecated))。 嗯,

我对这个东西有点困惑!

有人知道这件事吗?

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 seen the __OSX_AVAILABLE_BUT_DEPRECATED pre-processor macro, which is mapped to __AVAILABILITY_INTERNAL, which is mapped to __attribute__((deprecated))...

Well i'm a bit confused with this stuff!

Anybody knows something about that ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倾城花音 2024-10-23 09:43:50

__attribute__((已弃用))

普通函数的语法为

__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 还提供了 标头,该标头提供了 DEPRECATED_ATTRIBUTE 和 DEPRECATED_MSG_ATTRIBUTE (msg) 扩展为上述属性的宏,或者如果编译器不支持属性则什么也不包含。请注意,此标头在 OS X / iOS 之外不存在。


旁注,如果您使用 Swift,则使用 @available 属性 弃用某个项目,例如

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

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


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() {
    ...
}
忆梦 2024-10-23 09:43:50

您还可以使用更具可读性的 define 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

You can also use more readable define DEPRECATED_ATTRIBUTE

It defined in usr/include/AvailabilityMacros.h:

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

Objective-C methods example:

@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

You can also mark the whole class as deprecated:

DEPRECATED_ATTRIBUTE
@interface DeprecatedClass : NSObject { ... }
...
@end
究竟谁懂我的在乎 2024-10-23 09:43:50

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 文档:属性

Swift 5.0

Deprecate any method/class/struct/protocols using @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.")

Possible params:

  • introduced
  • deprecated
  • obsoleted
  • message
  • renamed

For more info see apple doc: Attributes

太阳公公是暖光 2024-10-23 09:43:50

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

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

If you are using C++14 in your xcode project, you may also use the [[deprecated]] or [[deprecated("reason")]] attribute that is now part of the language.

see documentation: http://en.cppreference.com/w/cpp/language/attributes

指尖微凉心微凉 2024-10-23 09:43:50

- 对于 SWIFT 代码:

将其放在方法的正上方:
@available(*,已弃用:<#Version#>,消息:<#Message#>)

示例:

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

- FOR SWIFT CODE:

Put this right above the method:
@available(*, deprecated: <#Version#>, message: <#Message#>)

example:

@available(*, deprecated: 11, message: "Use color assets instead")
public struct ColorPaletteItemResource: ColorPaletteItemResourceType {
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文