Convert to Swift 3 renamed my own Objective-C method

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

I recently converted to Swift 3, and it updates several API calls due to all the renaming that happened for Swift 3. This is great; I understand.

But not surprisingly, Swift 3 seems to have renamed the method to one of my Objective-C classes. I own the Objective-C class, and I named the method what I wanted: readDeliveryInfoItems . But now, after converting to Swift 3, I can no longer call .readDeliveryInfoItems() in my Swift class. He told me that it was renamed to .readItems() .

It does not make sense. And the Objective-C class still calls the readDeliveryInfoItems method, so there is something under the covers here.

I tried to rename the Objective-C readDeliveryInfoItems to readDeliveryInfo , building (Swift does not work because it says the readInfo() method does not exist, which is good) and then renames the method back to readDeliveryInfoItems . However, when I build after this, Swift returns to the idea that the method is called readInfo() . I was hoping this would cost Xcode to upgrade the Swift bridge and rename the method back to the correct name readDeliveryInfoItems() , but that is not the case.

How can i fix this?

UPDATE ADD MORE INFORMATION

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

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

But in the Generated Interface (see MartinR comment below) for this class, this is a function declaration:

 open func readItems() -> [XMPPDeliveryInfoItem]? 

There are other functions in this class that are similar to the readDeliveryInfoItems function, for example:

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

And they look right in the Generated Interface:

 open func sentDeliveryInfoItems() -> [XMPPDeliveryInfoItem]? 

Therefore, I cannot understand why I have this problem with only one function.

+5
source share
2 answers

The translation process is described in detail in

The relevant part for your question is (my attention):

Trim the type match with the base name of the method, so if the match begins with after the verb. For instance,

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

becomes:

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

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

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

but not VERB(sent) . This explains why - slightly simplifying your example -

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

becomes

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

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

You can override the mapping with NS_SWIFT_NAME :

 -(void)readDeliveryInfoItems NS_SWIFT_NAME(readDeliveryInfoItems()); 
+9
source

Roughly speaking (and I simplify), if the method name suffix is ​​a verb object corresponding to the return type, the suffix is ​​discarded.

A simple example would be a method called readString that returns an NSString.

Your method falls into these parameters (I said that I simplify, but you can see, roughly speaking, how true this is), so you get treatment.

Personally, I consider this a mistake, especially because in some cases a change can lead to a name conflict and make a method call impossible (especially if the Objective-C API does not belong to you and you cannot change it). For example, see this question: Swift 3 (Omit Needless Words), resulting in two functions having the same name

0
source

Source: https://habr.com/ru/post/1258548/


All Articles