I've wrote some functions (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)
}
And call them with:
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)
As you can see, when a type is the only parameter, it can omit the ".self". But for all functions that not have only a type parameter, they require the ".self".
I want to know:
- Why is That?
- And how to declare a function with multiple parameters that doesn't need ".self" where using it?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…