I 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 name 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 ".self". But for all functions that do not have just a type parameter, they require ".self".
I want to know:
- Why is this?
- And how to declare a function with several parameters that are not needed by ".self", where to use it?
ios swift
xfx
source share