You cannot pass a type closure to a type String->Voidparameter AnyObject->Void.
However, you can define a generic function:
func saveWithCompletionObject<T>(obj : T, success : T -> Void, failure : Void -> Void) {
success(obj)
}
Now the compiler can verify that it objhas the same type as the parameter success, for example:
func doSomething(success : String -> Void, failure : Void -> Void)
{
saveWithCompletionObject("Example", success, failure)
}
func doSomethingElse(success : Int -> Void, failure : Void -> Void)
{
saveWithCompletionObject(13, success, failure)
}
But I would recommend that I saveWithCompletionObjectjust take the Void->Void
parameter (no generics):
func saveWithCompletionObject(success : Void -> Void, failure : Void -> Void) {
success()
}
and the caller wraps it closes:
func doSomething(success : String -> Void, failure : Void -> Void)
{
saveWithCompletionObject( { success("Example") } , failure)
}
func doSomethingElse(success : Int -> Void, failure : Void -> Void)
{
saveWithCompletionObject( { success(13) }, failure)
}
, .
:
func andNowForSomethingCompletelyDifferent(success : (Int, Double) -> Void, failure : Void -> Void)
{
saveWithCompletionObject( { success(13, M_PI) }, failure)
}