swift类作为参数没有.self

我写了一些函数(Swift 2.1,XCode 7.1.1):

public func test1<T:UIView>(type: T.Type) -> T { print(type.dynamicType) return type.init() } public func test2<T:UIView>(type: T.Type)(_ n: Int) -> T { print(type.dynamicType) return type.init() } public func test3<T1:UIView, T2:UIView>(type1: T1.Type, _ type2: T2.Type) { print(type1.init()) print(type2.init()) } public func test4<T:UIView>(type: T.Type, _ n: Int) { print(type.init()) print(n) } public func test5<T:UIView>(n: Int,_ type: T.Type) { print(type.init()) print(n) } 

并与他们打电话给:

 test1(UIButton) test1(UIButton.self) test2(UIButton)(1) test2(UIButton.self)(1) test3(UIButton.self, UITextField.self) test4(UIButton.self, 1) test5(1, UIButton.self) 

正如你所看到的,当一个types是唯一的参数时,它可以省略“.self”。 但是对于不具有types参数的所有函数,它们都需要“.self”。

我想知道:

  1. 这是为什么?
  2. 以及如何使用多个参数声明一个不需要使用“.self”的函数?

巧合的是,这只是迅速演变而来的。 当Type是唯一参数时,省略.self的能力已经被报告为Swift中的一个bug 。

苹果的相关报价:

这是一个错误。 .self应该是无处不在的。 不幸的是,我们已经有了一段时间的错误,所以我不知道有多less代码会因为改变而破坏。 如果我们想要消除这个要求,那么这将被视为一种语言的变化,并且必须经过Swift Evolutionstream程。