我已经定义了一些响应模型
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
关于swift - 如何使用泛型推断类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44560158/