Swift:传递类型作为参数

Swift:传递类型作为参数,swift,types,functional-programming,Swift,Types,Functional Programming,是否可以在Swift中将类型作为函数参数传入?注意:我不想传入指定类型的对象,而是传入类型本身。例如,如果我想将Swift的复制为?功能: infix operator <-? { associativity left } func <-? <U,T>(x:U?, t:T) -> T? { if let z = x as? t { return z } } } infix操作符您可以使用T.Type,但必须强制转换为T,而不是T: in

是否可以在Swift中将类型作为函数参数传入?注意:我不想传入指定类型的对象,而是传入类型本身。例如,如果我想将Swift的
复制为?
功能:

infix operator <-? { associativity left }
func <-? <U,T>(x:U?, t:T) -> T? {
  if let z = x as? t {
      return z
    }
  }
}

infix操作符您可以使用
T.Type
,但必须强制转换为
T
,而不是
T

infix operator <-? { associativity left }
func <-? <U,T>(x:U?, t:T.Type) -> T? {
    if let z = x as? T {
        return z
    }
    return nil
}

中缀运算符啊,是的,就是这样。谢谢
[1,2, 3] <-? NSArray.self // Prints {[1, 2, 3]}
[1,2, 3] <-? NSDictionary.self // Prints nil