0

why


-(void)addSimpleListener:(id<XXSimpleListener>)listener

convert to swift look like this:

func add(_ listener: XXSimpleListener?) {   
}

but change the method to this


-(void)addSimpleListener:(id<XXSimpleListening>)listener


and it will convert to this

func addSimpleListener(_ listener: XXSimpleListening?){
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0x0
  • 43
  • 6
  • 2
    You mean "why Xcode will automatically rename the method to `add` in the first case?" – Sweeper May 18 '20 at 08:11
  • Possibly helpful (if not duplicate): [Converting to Swift 3 renamed my own Objective-C method](https://stackoverflow.com/questions/40157131/converting-to-swift-3-renamed-my-own-objective-c-method). – Martin R May 18 '20 at 08:17

1 Answers1

0

Xcode (or whatever tool you are using to do the conversion) is merely following Swift API guidelines. Specifically:

Omit needless words. Every word in a name should convey salient information at the use site.

More words may be needed to clarify intent or disambiguate meaning, but those that are redundant with information the reader already possesses should be omitted. In particular, omit words that merely repeat type information.

In the first case, the words SimpleListener in addSimpleListener is repeating the type of the parameter, so they are removed from the method name. However, in the second case, SimpleListener and SimpleListening does not look the same to whatever tool you are using, so it thinks that SimpleListener should be kept.

In my (human) opinion though, I think the method should be named addListener, because:

Occasionally, repeating type information is necessary to avoid ambiguity, but in general it is better to use a word that describes a parameter’s role rather than its type.

Listener is the role of the parameter.

Community
  • 1
  • 1
Sweeper
  • 213,210
  • 22
  • 193
  • 313