如何在Swift中创建通用闭包 [英] How to create generic closures in Swift

查看:100
本文介绍了如何在Swift中创建通用闭包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在swift中创建一个使用通用闭包(块)的函数。



我的第一次尝试是:

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



但是一旦我用另一个块调用这个函数,例如:

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

我得到一个错误:'AnyObject'字符串



提前感谢!

解决方案

您不能将类型 String-> Void 的关闭传递给类型 AnyObject-> Void的参数



但是,您可以定义一个泛型函数:

  func saveWithCompletionObject< T>(obj:T,success:T  - 空隙,失败:Void  - > Void){
// ...
success(obj)
}


$ b b

现在编译器可以验证 obj success 的参数具有相同的类型
,例如:

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

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

我建议 saveWithCompletionObject 只需要一个 Void-> Void
参数(没有泛型) / p>

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

/ em>包装其闭包:

  func doSomething(success:String  - >空隙,失败:Void  - > Void)
{
saveWithCompletionObject({success(Example)},失败)
}

func doSomethingElse(success:Int-& Void - > Void)
{
saveWithCompletionObject({success(13)},失败)
}

这更灵活,例如对于具有多个
参数的回调函数:

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

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 !

解决方案

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)
}

这篇关于如何在Swift中创建通用闭包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆