我将 swift 类与我的 Objective-C 代码混合在一起。使用 Swift 2.3,一切都很好,并且按预期工作。
我最近转换到 Swift 3,由于 Swift 3 发生的所有重命名,它更新了几个 API 调用。这很好;我明白了。
但不妙的是 Swift 3 似乎重命名了我的一个 Objective-C 类中的一个方法。我拥有 Objective-C 类,我调用了我想要的方法:readDeliveryInfoItems
。但是现在,在转换为 Swift 3 之后,我不能再在我的 Swift 类中调用 .readDeliveryInfoItems()
了。它告诉我它已重命名为 .readItems()
。
这毫无意义。并且 Objective-C 类仍然调用方法 readDeliveryInfoItems
,所以这里有一些隐藏的东西。
我尝试将 Objective-C readDeliveryInfoItems
方法重命名为 readDeliveryInfo
,构建(Swift 失败,因为它说 readInfo()
方法不存在,这很好),然后将该方法重命名回 readDeliveryInfoItems
。然而,当我在此之后构建时,Swift 回过头来认为该方法被称为 readInfo()
。我希望这会欺骗 Xcode 刷新 Swift 桥接并将方法重命名为正确的名称 readDeliveryInfoItems()
,但它没有。
我该如何解决这个问题?
更新以添加更多信息
我的 Objective-C 类的接口(interface)有这个函数声明:
- (nullable NSArray<XMPPDeliveryInfoItem *> *)readDeliveryInfoItems;
但是在该类的生成接口(interface)(参见下面 MartinR 的评论)中,函数声明改为:
open func readItems() -> [XMPPDeliveryInfoItem]?
该类中还有其他类似于readDeliveryInfoItems
函数的函数,比如这个:
- (nullable NSArray<XMPPDeliveryInfoItem *> *)sentDeliveryInfoItems;
它们在生成的界面中看起来是正确的:
open func sentDeliveryInfoItems() -> [XMPPDeliveryInfoItem]?
所以我不明白为什么只有一个函数会出现这个问题。
最佳答案
翻译过程在
中有详细描述你的问题的相关部分是(强调我的):
Prune a match for the enclosing type from the base name of a method so long as the match starts after a verb. For example,
extension UIViewController { func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)? = nil) }
becomes:
extension UIViewController { func dismissAnimated(flag: Bool, completion: (() -> Void)? = nil) }
据我所知,这个剪枝算法是在 StringExtras.cpp (并使用了很多启发式方法), 和 PartsOfSpeech.def 包含被视为动词的单词列表,例如
VERB(dismiss)
VERB(read)
VERB(send)
但不是 VERB(sent)
。这就解释了为什么——稍微简化你的例子——
@interface DeliveryInfo : NSObject
-(void)readDeliveryInfoItems;
-(void)sentDeliveryInfoItems;
@end
成为
open class DeliveryInfo : NSObject {
open func readItems()
open func sentDeliveryInfoItems()
}
类型名称在动词“read”之后被删减,但不在
非动词“发送”。 (您可以通过更改第二种方法来验证
名称为 sendDeliveryInfoItems
,然后映射到 sendItems()
。)
您可以使用 NS_SWIFT_NAME
覆盖映射:
-(void)readDeliveryInfoItems NS_SWIFT_NAME(readDeliveryInfoItems());
关于objective-c - 转换为 Swift 3 重命名了我自己的 Objective-C 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40157131/