Swift syntax for block with completionHandler... in delegate method Swift syntax for block with completionHandler... in delegate method swift swift

Swift syntax for block with completionHandler... in delegate method


  1. a void function is (Type1, Type2)->()
  2. you need to add the ->() for the method itself
  3. You need to call completionHandler with (cred, nil), not return a tuple

    func provideUsernamePasswordForAuthChallenge(authChallenge: NSURLAuthenticationChallenge!,  completionHandler:(NSURLCredential?, NSError?)->()) ->() {   var cred =       NSURLCredential(user: "myname", password: "mypass",          persistence: NSURLCredentialPersistence.ForSession)   completionHandler(cred, nil)}


This code may help ->

 AFAppDotNetAPIClient.sharedClient().GET("User", parameters:nil, success:{ (task : NSURLSessionDataTask!, JSON: AnyObject!) in        // within block....        }, failure:nil)


Your type is backwards — you want the completion handler to take two arguments and return none, but you've defined it as a function that takes no arguments and returns a pair. Flop the two sides of the arrow.