Swift 如何使用泛型推断类型?

Swift 如何使用泛型推断类型?,swift,generics,Swift,Generics,我将一些响应模型定义为 class UserProfile{ var name:String = "Anish" var age:Int = 20 } class StudentProfile:UserProfile{ var std_id:Int = 1 } 我有一个Web API请求类 class WebCaller{ class func callWebService<T>(responseType:T.Type,completion

我将一些响应模型定义为

class UserProfile{

    var name:String = "Anish"
    var age:Int = 20
}

class StudentProfile:UserProfile{

    var std_id:Int = 1

}
我有一个Web API请求类

class WebCaller{

    class func callWebService<T>(responseType:T.Type,completionHandler:(T)->()){

        let model =  StudentProfile() as! responseType//wont compile here
        completionHandler(model)
    }
}
现在如果我想打电话给你

 WebCaller.callWebService(responseType: StudentProfile.self){ responseModel in
            //response model should automatically infer the type 
            print(responseModel.name)
            print(responseModel.age)
            print(responseModel.std_id)
        }
 WebCaller.callWebService(responseType: UserProfile.self){ responseModel in
            //this should automaticaly infer the type
            print(responseModel.name)
            print(responseModel.age)
        }
但是,这可以通过使用任意对象类型来实现

class func callWebService<T>(responseType:T.Type,completionHandler:(AnyObject)->())
类func callWebService(响应类型:T.Type,completionHandler:(AnyObject)->())

但是我需要将其强制转换为我的响应类型。我试图实现的是,我的API调用者应该向我发送类型,这样我就不应该在我的
视图模型中强制转换它。类型几乎不可用,您不需要在您的场景中使用任何类型的泛型

class A{
    let typeA = "property typeA"
}
class B{
    let typeB = "property typeB"
}
class C{}

func f0(object: AnyObject) {

        if let a = object as? A {
            print(1, a.typeA)
            return
        }

        if let b = object as? B {
            print(2, b.typeB)
            return
        }
        print("unknown type")
}

f0(object: A())
f0(object: B())

func f1(object: AnyObject) {
    switch object {
    case is A:
        let a = object as! A // will never fail!!
        print(3, a.typeA)
    case is B:
        let b = object as! B // will never fail!!
        print(4, b.typeB)
    default:
        print("unknown type")
        break
    }
}

f1(object: A())
f1(object: B())

f0(object: C())
f1(object: C())
它打印

1 property typeA
2 property typeB
3 property typeA
4 property typeB
unknown type
unknown type
1 property typeA
I am printable B
2 property typeB
I am printable C
unknown type
要查看A和A.Type之间的关系,请将我们现在的代码片段与下一个代码片段进行比较,这将给出相同的结果

func f3(object: AnyObject) {
    switch type(of: object) {
    case is A.Type:
        let a = object as! A // will never fail!!
        print(5, a.typeA)
    case is B.Type:
        let b = object as! B // will never fail!!
        print(6, b.typeB)
    default:
        print("unknown type")
        break
    }
}
使用第一个场景可能更实际,因为在对象中构建了一些公共功能

protocol Printable {
}
extension Printable {
    func p(){
        print("I am printable", type(of: self))
    }
}
class A{
    let typeA = "property typeA"
}
class B: Printable{
    let typeB = "property typeB"
}
class C: Printable{}

func f0(object: AnyObject) {
    if let p = object as? Printable {
        p.p()
    }

    if let a = object as? A {
        print(1, a.typeA)
        return
    }

    if let b = object as? B {
        print(2, b.typeB)
        return
    }

    print("unknown type")
}

f0(object: A())
f0(object: B())
f0(object: C())
它打印

1 property typeA
2 property typeB
3 property typeA
4 property typeB
unknown type
unknown type
1 property typeA
I am printable B
2 property typeB
I am printable C
unknown type

为什么不将model=StudentProfile()设为!T
解决了你的问题?是的,确实如此……但这就是解决问题的方法吗?对不起,这与整个动机相矛盾……我不想每次都演…@Costello,那你想要什么?很快或者很晚你需要施放结果才能使用它,不是吗?