转换为 Swift 3 重命名了我自己的 Objective-C 方法 [英] Converting to Swift 3 renamed my own Objective-C method

查看:20
本文介绍了转换为 Swift 3 重命名了我自己的 Objective-C 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 swift 类与我的 Objective-C 代码混合在一起.使用 Swift 2.3,一切都很好,并且按预期工作.

I have swift classes mixed in with my Objective-C code. With Swift 2.3, everything was fine and worked as expected.

我最近转换为 Swift 3,由于 Swift 3 发生的所有重命名,它更新了几个 API 调用.我明白了.

I recently converted to Swift 3, and it updates several API calls because of all the renaming that occurred for Swift 3. That's fine; I get that.

但不好的是,Swift 3 似乎重命名了 Objective-C 类中的一个方法.我拥有Objective-C 类,我调用了我想要的方法:readDeliveryInfoItems.但是现在,在转换为 Swift 3 之后,我不能再在我的 Swift 类中调用 .readDeliveryInfoItems() 了.它告诉我它已重命名为 .readItems().

But what's not fine is that Swift 3 seems to have renamed a method in one of my Objective-C classes. I own the Objective-C class and I called the method what I wanted: readDeliveryInfoItems. But now, after converting to Swift 3, I can't call .readDeliveryInfoItems() anymore in my Swift class. It's telling me it has been renamed to .readItems().

这没有意义.而且Objective-C 类仍然调用方法readDeliveryInfoItems,所以这里有一些隐藏的东西.

That makes no sense. And the Objective-C class still calls the method readDeliveryInfoItems, so there is something under the covers going on here.

我尝试将 Objective-C readDeliveryInfoItems 方法重命名为 readDeliveryInfo,构建(Swift 失败,因为它说 readInfo() 方法不存在,这很好),然后将该方法重命名回 readDeliveryInfoItems.但是,当我在此之后构建时,Swift 又回到认为该方法称为 readInfo().我希望这会欺骗 Xcode 刷新 Swift 桥接并将方法重命名回正确的名称 readDeliveryInfoItems(),但它没有.

I have tried renaming the Objective-C readDeliveryInfoItems method to readDeliveryInfo, building (Swift fails because it says that the readInfo() method doesn't exist, which is good), and then renaming the method back to readDeliveryInfoItems. However, when I build after this, Swift goes back to thinking the method is called readInfo(). I was hoping this would trick Xcode into refreshing the Swift bridging and renaming the method back to the correct name readDeliveryInfoItems(), but it did not.

我该如何解决这个问题?

How can I fix this?

更新以添加更多信息

我的Objective-C类的接口有这个函数声明:

The interface of my Objective-C class has this function declaration:

- (nullable NSArray<XMPPDeliveryInfoItem *> *)readDeliveryInfoItems;

但是在该类的生成接口(见下面 MartinR 的评论)中,函数声明是这样的:

But in the Generated Interface (see MartinR's comment below) for that class, the function declaration is this instead:

open func readItems() -> [XMPPDeliveryInfoItem]?

该类中还有其他类似于 readDeliveryInfoItems 函数的函数,例如这个:

There are other functions in that class that are similar to the readDeliveryInfoItems function, such as this one:

- (nullable NSArray<XMPPDeliveryInfoItem *> *)sentDeliveryInfoItems;

它们在生成的接口中看起来是正确的:

And they look correct in the Generated Interface:

open func sentDeliveryInfoItems() -> [XMPPDeliveryInfoItem]?

所以我不明白为什么我只有一个函数会出现这个问题.

So I can't figure out why I'm having this problem with only the one function.

推荐答案

翻译过程在

您问题的相关部分是(强调我的):

The relevant part for your question is (emphasis mine):

从方法的基本名称中修剪封闭类型的匹配,以便只要匹配开始在动词之后.例如,

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

变成:

extension UIViewController {
  func dismissAnimated(flag: Bool, completion: (() -> Void)? = nil)
}

这个修剪算法——据我所知——在StringExtras.cpp(并使用了很多启发式方法),和 PartsOfSpeech.def包含被视为动词的单词列表,例如

This pruning algorithm is – as far as I can see – implemented in StringExtras.cpp (and uses a lot of heuristics), and PartsOfSpeech.def contains a list of words which are considered a verb, such as

VERB(dismiss)
VERB(read)
VERB(send)

不是 动词(发送).这就解释了为什么——稍微简化你的例子——

but not VERB(sent). That explains why – simplifying your example slightly –

@interface DeliveryInfo : NSObject
-(void)readDeliveryInfoItems;
-(void)sentDeliveryInfoItems;
@end

变成

open class DeliveryInfo : NSObject {
    open func readItems()
    open func sentDeliveryInfoItems()
}

类型名称在动词read"之后被修剪,而不是在动词read"之后非动词发送".(您可以通过更改第二种方法来验证名称到 sendDeliveryInfoItems,然后映射到 sendItems().)

The type name is pruned after the verb "read", but not after the non-verb "sent". (You can verify that by changing the second method name to sendDeliveryInfoItems which is then mapped to sendItems().)

您可以使用 NS_SWIFT_NAME 覆盖映射:

You can override the mapping with NS_SWIFT_NAME:

-(void)readDeliveryInfoItems NS_SWIFT_NAME(readDeliveryInfoItems());

这篇关于转换为 Swift 3 重命名了我自己的 Objective-C 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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