ios - swift method accept parameter if conform a protocol -


i have protocol named webserviceprotocol , method :

class func executerequest(delegate:__something__,url:string,postparameters:[string:string],headerparameters:[string:string]){    //do , call delegate } 

i want have conditional argument check if input conform webserviceprotocol accept it.

i'm tying write global class function work every input , maybe viewcontroller or nsobject class.

in java classes did :

<? extends someclass> 

edit enter image description here webservice.swift

import foundation  protocol webserviceprotocol {     func ondatareceived(data:nsdictionary!)     func onfailure() }  class webservice:nsobject{      class func executerequest(delegate:webserviceprotocol,url:string,postparameters:[string:string],headerparameters:[string:string]){         if let url: nsurl = nsurl(string: url){             var request:nsmutableurlrequest = nsmutableurlrequest(url:url)             request.httpmethod = "post"             request.httpbody = arraytohttpparams(postparameters).datausingencoding(nsutf8stringencoding)             nsurlconnection.sendasynchronousrequest(request, queue: nsoperationqueue.mainqueue()){                     response, data, error in                     if error == nil {                         if let jsonstring = nsstring(data: data, encoding: nsutf8stringencoding){                             if let dictionary = helper.parsejson(jsonstring string) {                                 delegate.ondatareceived(dictionary)                                 println(dictionary)                                 return;                             }                         }                         println("failure !")                         delegate.ondatareceived(nil)                     }                     else{                         delegate.onfailure()                     }             }         }     }      class func arraytohttpparams(params:[string:string])->string{         var mergedparams = [string]();         (key,value) in params {             mergedparams.append(key+"="+value)         }         return "&".join(mergedparams);     } } 

authentication.swift

protocol authenticationloginprotocol {     func onsuccess(data:nsdictionary)     func onfailure(data:nsdictionary) }  class authentication:nsobject,webserviceprotocol{     func attemplogin(delegate:viewcontroller,emial:string,password:string){          var params = [string:string]();         params["key1"]="value1"         params["key2"]="value2"          webservice.executerequest(self, url: "", postparameters: params, headerparameters: params)      }      func ondatareceived(data: nsdictionary!) {      }     func onfailure() {      }  } 

update

you trying reference instance in type method. instead of this,

webservice.executerequest(self, url: "", postparameters: params, headerparameters: params) 

write this:

webservice.executerequest(authentication.self, url: "", postparameters: params, headerparameters: params) 

then later in example authentication:

class authentication: nsobject, webserviceprotocol {      // ...      func somemethod() {         webservice.executerequest(self, url: "", postparameters: ["1": "2"], headerparameters: ["1": "2"])     }      // ... } 

original answer

you make generic function type constraint protocol:

func myfunc<t: webserviceprotocol>(input: t) {   // input surely conforms webserviceprotocol } 

this way, t can type, long conforms webserviceprotocol.

for more details, see type constraints here.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -