嵌套闭包中的方法参数 [英] Method parameters in nested closure

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

问题描述

我想了解传递给方法的参数如何可用于嵌套闭包。我很紧张,我写的东西并不总是有原始的参数。

I am trying to understand how parameters passed to a method are available to nested closures. I'm nervous that something I wrote won't always have the original parameters available.

(这些都是非常简化的例子)

(these are drastically simplified examples)

我写了一个方法来指定一个闭包作为参数:

I have a method that I wrote that specifies a closure as a parameter:

func saveNameAndAgeToServer(serverParams: [String:String], completionHandler: (age: NSNumber) -> ()) {

    // Connect to a server
    // POST a name and dob from serverParams
    // Receives a current age in completion:

    completionHandler(age: 22)
}

现在在其他地方我创建另一个方法,也指定一个闭包,接受两个参数,并调用第一个函数:

Now somewhere else I create another method, that also specifies a closure, takes two parameters and calls the first function:

func AwesomeFunc(name: String, dob: NSDate, completionHandler: (isOverTwentyOne: Bool) -> ()) {

    let formattedDob = NSDateFormatter().stringFromDate(dob)

    saveNameAndAgeToServer([name:formattedDob]) { (age) -> () in

        if (age as Int) >= 21 {

            print("\(name) can have a beer.")
            completionHandler(isOverTwentyOne: true)

        } else {

            print("\(name) is too young to drink, he can have a water.")
            completionHandler(isOverTwentyOne: false)
        }
    }

}


$ b b

我能保证传递给后一个函数的参数(name和dob)总是可用的吗?

Am I able to guarantee that the parameters (name and dob) passed into this latter function will always be available?

我想要的是将saveNameAndAgeToServer关闭运行的内存始终具有可用的AwesomeFunc的参数吗?我很确定的功能是所有的举行,而无论它调用是完成,但会喜欢第二意见。

What I'm trying to ask is will the memory that the saveNameAndAgeToServer closure runs within always have the parameters of AwesomeFunc available to it? I'm pretty sure the function is all being held while whatever it calls is completed but would love a 2nd opinion.

推荐答案

你是正确的,捕获的变量将持续到闭包的生命周期。下面是从apple的swift文档捕获变量的动作:

You are correct, captured variables will last for the life of the closure. Here's an exert on capturing variables from apple's swift documentation:


闭包可以从周围的
上下文中捕获常量和变量,它被定义。然后,closure可以引用和
修改这些常量和变量在
体中的值,即使定义常量和
变量的原始作用域不再存在。

A closure can capture constants and variables from the surrounding context in which it is defined. The closure can then refer to and modify the values of those constants and variables from within its body, even if the original scope that defined the constants and variables no longer exists.

在Swift中,可以捕获值的闭包的最简单形式是一个
嵌套函数,它写在另一个函数体中。嵌套的
函数可以捕获其任何外部函数的参数,并且
也可以捕获外部
函数中定义的任何常量和变量。

In Swift, the simplest form of a closure that can capture values is a nested function, written within the body of another function. A nested function can capture any of its outer function’s arguments and can also capture any constants and variables defined within the outer function.

https:/ /developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

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

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