Fast class as parameter without .self

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?
+1
ios swift
source share
1 answer

By the way, it just came across a rapid evolution. The ability to omit .self when Type is the only argument has been indicated as a bug in Swift.

Relevant quote from Apple:

This is mistake. .self assumed everywhere. Unfortunately, we had an error for a while, so I'm not sure how much code we will break by changing it. If we want to remove the requirement that will be considered a change in language and to go through the process of rapid evolution.

+2
source share

All Articles