spinhaxo spinhaxo - 4 months ago 15
Swift Question

iOS Swift function with type () -> ()

Im unable to create function of type () -> () in Swift with xCode...

so far im at

func successBlock() -> Void {
//code
return ()
}


but this is only () not () -> ()

I want this function to fit the type () -> () because of this function:

func loginToFacebookWithSuccess(callingViewController: UIViewController, successBlock: () -> (), andFailure failureBlock: (NSError?) -> ()) {


gonna pass the successBlock() func as variable in there.
I have nearly the same problem with the failureBlock

or am I on the wrong way?

Thank you, spinhaxo

Answer

successBlock is a function.
You need closures.

let successBlock: () -> () = {
  // do something here...
}

let failureBlock: (NSError?) -> () = { error in
  // do something with the error...
}

If you don't want to store them, you can just define them while calling the method like this:

loginToFacebookWithSuccess(
  callingController,
  successBlock: {
    // do something here...
  }, andFailure: { error in
    // do something with the error...
  }
)