我已将一些响应模型定义为
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)
}
但是,这可以使用AnyObject类型
来完成class func callWebService<T>(responseType:T.Type,completionHandler:(AnyObject)->())
但是我需要将它转换为我的响应类型。我想尝试的是我的API调用者应该向我发送类型,以便我不应该将其投射到我的ViewModel
答案 0 :(得分:0)
T.Type几乎无法使用,您不需要在您的方案中使用任何类型的泛型
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
看看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
I am printable B
2 property typeB
I am printable C
unknown type