How to create generic closures in Swift

孤人 提交于 2019-12-13 15:07:04

问题


I would like to create a function that uses generic closure (block) in swift.

My first attempt was to do like :

func saveWithCompletionObject(obj : AnyObject, success : AnyObject -> Void, failure : Void -> Void)

But as soon as I call this function with another block, such as :

func doSomething(success : String -> Void, failure : Void -> Void)
{
    saveWithCompletionObject("Example", success, failure)
}

I get an error : 'AnyObject' is not a subtype of 'String'

Thanks in advance !


回答1:


You cannot pass a closure of type String->Void to a parameter of type 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 obj has the same type as the parameter of 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 saveWithCompletionObject just takes a Void->Void parameter (without generics):

func saveWithCompletionObject(success : Void -> Void, failure : Void -> Void) {
    // ...
    success()
}

and the caller wraps its closure:

func doSomething(success : String -> Void, failure : Void -> Void)
{
    saveWithCompletionObject( { success("Example") } , failure)
}

func doSomethingElse(success : Int -> Void, failure : Void -> Void)
{
    saveWithCompletionObject( { success(13) }, failure)
}

This is more flexible, e.g. for callback functions with more than one parameter:

func andNowForSomethingCompletelyDifferent(success : (Int, Double) -> Void, failure : Void -> Void)
{
    saveWithCompletionObject( { success(13, M_PI) }, failure)
}


来源:https://stackoverflow.com/questions/27274141/how-to-create-generic-closures-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!