在swift中传入一个类型作为参数

时间:2015-11-01 21:11:07

标签: ios swift uiviewcontroller

我制作的方法可以在Swift中动画从一个视图控制器到另一个视图控制器的过渡。

从方法调用到方法调用的唯一不同之处是UIViewController的特定子类。

因此,我假设我需要传递的唯一方法是让方法以我想要的方式工作,而不是UIViewController的子类的实例,而只是子类类型本身。我如何在Swift中写这个?

 func doAnimation(fromType, toType) { //don't pass in instances, but types to be instantiated (this is the part that I need help with)
      ... //declare fromVC and toVC 
        let topVC = !self.isPresenting ? fromVC as fromType : toVC as toType //assert what type top view controller will be be
        let bottomVC = !self.isPresenting ? fromVC as! fromType : toVC as toType //assert what type bottom view controller will be
      ...

}

提前谢谢。

1 个答案:

答案 0 :(得分:-1)

import Foundation

class C {}

func f<T>(type: T) {
    switch type {
    case is Int.Type:
        print("Integer type")
    case is String.Type:
        print("String type")
    case is C.Type:
        print("C type")
    default:
        print("something else ... :-)")
    }
}

f(Int.self)     // Integer type
f(C.self)       // C type
f(String.self)  // String type
f(Any.self)     // something else ... :-)