在swift中使用具有多个参数的闭包 [英] Usage of closures with multiple arguments in swift

查看:109
本文介绍了在swift中使用具有多个参数的闭包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题主要基于这个问题:

This question is largely based on this one:

链接

主要的区别是我想传递参数给闭包。说我有这样的:

The main difference being that I want to pass in arguments to the closure as well. Say I have something like this:

func someFunctionThatTakesAClosure(completionClosure: (venues: Dictionary<String, AnyObject>, error: NSError) -> ()) {
        // function body goes here
        var error: NSError?
        let responseDictionary: Dictionary<String, AnyObject> = ["test" : "test2"]
        completionClosure(venues: responseDictionary, error: error!)
    }

这里没有错误。但是当我在我的主视图控制器中调用此函数时,我尝试了几种方法,但所有的结果有不同的错误:

No error here. But when I call this function in my main view controller I have tried several ways but all of the result in different errors:

venueService.someFunctionThatTakesAClosure(completionClosure(venues: Dictionary<String, AnyObject>, error: NSError){

        })

或像这样:

venueService.someFunctionThatTakesAClosure((venues: Dictionary<String, AnyObject>, error: NSError){

        })

/ p>

or even like this:

venueService.someFunctionThatTakesAClosure(completionClosure: (venues: Dictionary<String, AnyObject>, error: NSError) -> (){

            });

我可能只是累了,但任何帮助将非常感谢! b $ b

I'm probably just way tired, but any help would be greatly appreciated!

推荐答案

venueService.someFunctionThatTakesAClosure({
  venues, error in
  // do stuff
})

您可以选择指定类型(但是由于编译器知道什么类型关闭应该提供,你不必严格地:

you can optionally specify the types (but since the compiler knows what types the closure is supposed to provide, you don't strictly have to:

venueService.someFunctionThatTakesAClosure({
  (venues: Dictionary<String, AnyObject>, error: NSError) -> () in
  // do stuff
})

但我看到你的调用代码中的另一个问题:

But I see another issue in your calling code:

completionClosure(venues: responseDictionary, error: error!)
//            No Bueno. What if there's no error?    ^^^^^^

你不应该强制解开错误,因为它是完全可能的,它仍然是零。强制解开nil将导致错误。所以你想要这样:

You shouldn't force unwrap the error since it's entirely possible that it's still nil. Force unwrapping nil will cause an error. So you want this:

completionClosure(venues: responseDictionary, error: error)

您想要更改您的闭包以接受可选错误。总计:

AND you want to change your closure to take an optional error. In total:

func someFunctionThatTakesAClosure(completionClosure: (venues: Dictionary<String, AnyObject>, error: NSError?) -> ()) {
    …
    completionClosure(venues: responseDictionary, error: error)
}

// when calling:
venueService.someFunctionThatTakesAClosure({
  (venues: Dictionary<String, AnyObject>, error: NSError?) -> () in
  // do stuff
})

如果你想传递额外的参数,请记住swift被优化为闭包作为最后一个参数传递(这是一个广泛遵循的约定in objective-c API):

If you'd like to pass additional arguments, keep in mind swift is optimized for closures to be passed as the last argument (and it's a widely followed convention in objective-c APIs):

// in venueService:
func someArgAndClosureFunction(arg1: String, arg2: Int, 
                  completionClosure: (venues: Dictionary<String, AnyObject>, error: NSError?) -> ()) {
    // do stuff
}

// When calling:
venueService.someArgAndClosureFunction("string arg 1", arg2: 10) {
  (venues: Dictionary<String, AnyObject>, error: NSError?) -> () in
  // do stuff
}

ve使用了尾随句法,它允许你传递外部函数调用parens(但它仍然作为最后一个参数传递)。

In this example I've used the trailing closure syntax which allows you pass the closure outside the function call parens (but it is still passed as the last argument).

这篇关于在swift中使用具有多个参数的闭包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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