0

My situation is similar to this below question related to .Net.

Value of type 'T' cannot be converted to TServiceResponse is My Code:

public class TServiceResponse : ResponseNode,CommsObject{
    public var type : String!
    public var Status : String?



    required public init?(_ dictionary: NSDictionary){
        self.type = dictionary.field("$type")
        self.Status = dictionary.field("Status")


    }

}
public enum WebResult<T : ResponseNode> {
            case Result(JsonResponse<T>)
            case Error(NSError)
}

func executePost<Message : RequestMessage, Node : ResponseNode>(request: JsonPostRequest<Message>,
                completionHandler: (result: WebResult<Node>) -> Void) -> NSURLSessionTask?{

if let jsonResponse : JsonResponse<TServiceResponse> =   self.createResponse(data,  error: &runtimeError){
    return completionHandler(result: (jsonResponse as? WebResult<Node>)!)
    }
}

I believe that what I ask for defeats the purpose of the Generics in the first place. But is there a hack to get this done? I need this hack to ensure that I do minimum changes to the code.

Community
  • 1
  • 1
RK-
  • 12,099
  • 23
  • 89
  • 155

1 Answers1

0

This should work :

let result =  runtimeError.code == 0 ? WebResult.Result(jsonResponse) : WebResult.Error(runtimeError)    
return completionHandler(result:result)
Alain T.
  • 40,517
  • 4
  • 31
  • 51