为什么Objective-C转换为快速错误

为什么


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

转换为快速外观,如下所示:

func add(_ listener: XXSimpleListener?) {   
}

但是将方法更改为此


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

它将转换为

func addSimpleListener(_ listener: XXSimpleListening?){
}
评论
  • qq情侣
    qq情侣 回复

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

    省略不必要的词。名称中的每个单词都应在使用地点传达重要信息。      可能需要更多的单词来阐明意图或歧义,但是应该省略那些对读者已经拥有的信息多余的词。特别是,省略仅重复类型信息的单词。

    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:

    有时,重复类型信息对于避免歧义是必要的,但是通常最好使用描述参数作用而不是其类型的词。

    Listener is the role of the parameter.