Swift: how to return class type from function

前端 未结 4 746
南方客
南方客 2021-02-03 20:08

I know it is possible to pass class type to a function in swift:

func setGeneric(type: T.Type){ }
setGeneric(Int.self)

But how we can

4条回答
  •  轮回少年
    2021-02-03 20:40

    It works when I modify your function like this:

    func getGeneric(object: T) -> T.Type {
        return T.self
    }
    
    getGeneric(0)    // Swift.Int
    

提交回复
热议问题