[英]How to infer type using 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,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
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.