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

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

问题描述

我在我的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似乎已经在 my 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.

我该如何解决?

更新以添加更多信息

我的Objective-C类的接口具有以下函数声明:

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

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

但是在该类的Generated Interface(请参见下面的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 translation process is described in detail in

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

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)

不是 VERB(sent).这就解释了为什么-稍微简化您的示例-

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"之后修剪,但不在 非动词已发送". (您可以通过更改第二种方法来验证 名称到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覆盖映射:

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

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

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