2017-06-15 148 views
0

我已经定义了一些响应模型作为如何使用泛型推断类型?

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)->()) 

,但我需要它转换为我的效应初探type.What我试图acheieve是我的API调用者应该给我的类型,这样我不应该投完成它在我的ViewModel

+0

为什么不这样'让模型= StudentProfile()的! T'解决你的问题? – Bilal

+0

是的,但...是要走的路? – Costello

回答

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 
+0

抱歉,这违背了整个motive..I不希望每次投... – Costello

+0

@Costello所以你想要什么?不久或以后,您需要将结果转换为可以使用的结果,是不是这样? – user3441734