gpt4 book ai didi

objective-c - Objective-C 有对应的 CF_SWIFT_NAME() 吗?

转载 作者:搜寻专家 更新时间:2023-10-31 08:25:12 25 4
gpt4 key购买 nike

有没有办法固定在 Swift 中为 Objective-C 实现的方法的名称?

一个Swift方法

static func convert(foo: Foo) -> Bar

Objective-C 中变为

+ (Bar *)convertWithFoo:(Foo *)foo;

当一个想要的名字是

+ (Bar *)convertFoo:(Foo *)foo;    // without `With`

从 Objective-C 到 Swift,我会使用 CF_SWIFT_NAME()NS_SWIFT_NAME() 宏。

最佳答案

是的,您可以使用 @objc 属性实现:

@objc(convertFoo:)
static func convert(foo: Foo) -> Bar { ... }

来自 "Attributes"在 Swift 引用中:

The objc attribute optionally accepts a single attribute argument, which consists of an identifier. Use this attribute when you want to expose a different name to Objective-C for the entity the objc attribute applies to. You can use this argument to name classes, enumerations, enumeration cases, protocols, methods, getters, setters, and initializers.

关于objective-c - Objective-C 有对应的 CF_SWIFT_NAME() 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40931405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com