ios - Swift Generics Inheritance -
i'm trying create utils make http request. want use :
var callback = callback<loginresultbean>(onsuccess: { (data) -> () in print("true"); }) { (error) -> () in print("false"); } var handler = connectionhandler<loginresultbean>(url : "https://.....", callback : callback);
loginbeanresult can change other model want retrieve server.
but doesn't compile cause of error : 'connectionhandler' requires 'loginresultbean' inherit 'basemodel'
but loginbeanresult inherit basemodel don't understand error.
here class code :
//basemodel.swift import objectmapper import foundation class basemodel : nsobject, mappable { required init?(_ map: map) { } func mapping(map: map) { } }
.
//loginbeanresult.swift import objectmapper import foundation class loginresultbean : basemodel { var jeton : string?; var etat : string?; var muna : string?; var idabo : string?; var codepostal : string?; var ville : string?; override func mapping(map: map) { super.mapping(map); self.jeton <- map["jeton"]; self.etat <- map["etat"]; self.muna <- map["muna"]; self.idabo <- map["idabo"]; self.codepostal <- map["codepostal"]; self.ville <- map["ville"]; } }
.
//connectionhandler.swift import objectmapper import foundation class connectionhandler<t: basemodel> { var url : string; var callback : callback<t>; init(url : string, callback : callback<t>) { self.url = url; self.callback = callback; } func sendrequest(params : basemodel) { let jsondata = mapper().tojsonstring(params, prettyprint:false)?.datausingencoding(nsutf8stringencoding); let postlength : string = string(jsondata!.length); let request : nsmutableurlrequest = nsmutableurlrequest(); let session = nsurlsession.sharedsession(); request.url = nsurl(string: url); request.httpmethod = "post"; request.setvalue(postlength, forhttpheaderfield: "content-length"); request.setvalue("application/json", forhttpheaderfield: "content-type"); request.httpbody = jsondata; let task = session.datataskwithrequest(request, completionhandler: {data, response, error -> void in let response = mapper<response<t>>().map(string(data: data!, encoding: nsutf8stringencoding)); let code = response?.entete?.code; let message = response?.entete?.message; if(code == 0) { self.callback.onsuccess(data: (response?.corp)!); } else { let error = nserror(domain: "connectionhandler", code: code!, userinfo: nsdictionary(object: message!, forkey: nslocalizeddescriptionkey) [nsobject : anyobject]); self.callback.onfailure(error: error); } }); task.resume(); } }
.
//callback.swift import objectmapper import foundation class callback<t: basemodel> { let onsuccess: (data : t) -> () let onfailure: (error : nserror) -> () init(onsuccess: (data : t) -> (), onfailure: (error : nserror) -> ()) { self.onfailure = onfailure self.onsuccess = onsuccess } }
Comments
Post a Comment