Reputation: 58
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
Upvotes: 1
Views: 111
Reputation: 2090
If you decided to go with func
, pass the function's name without the parentheses. Hence, pass successBlock
instead of successBlock()
.
loginToFacebookWithSuccess(someViewController, successBlock: successBlock, andFailure: failureBlock)
That's because the type of the function successBlock
is () -> ()
while its return type is only ()
.
Extra:
()
has an alias which is Void
: public typealias Void = ()
.You can even omit Void
nor ()
:
func successBlock() {
//code
//no return statement needed
}
func failureBlock(error: NSError?) {
//code
//no return statement needed
}
Upvotes: 2
Reputation: 2637
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...
}
)
Upvotes: 2
Reputation: 47886
Seems you are confusing a closure with the result of calling the closure.
You just need to pass successBlock
, which is a closure of type () -> ()
. (aka () -> Void
)
But successBlock()
represents the result of calling successBlock
, which may be a void value (aka ()
), not a closure.
Nearly the same for failureBlock
. Do not pass the result of calling closures, pass closures themselves.
Upvotes: 1