不了解Swift中的闭包示例 [英] Don't understand closures example in Swift

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

问题描述

我正在尝试了解swift和闭包。
我在这个例子中受阻。

I'm trying to learn about swift and closures. I'm stuck on this example.

 numbers.map({
    (number: Int) -> Int in
    let result = 3 * number
    return result
 })

什么是(数字:整数)->整数?是功能吗?它在哪里定义?
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html#//apple_ref/doc/uid/TP40014097-CH2-ID1

What is (number: Int) -> Int? Is it a function? Where is it defined? https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html#//apple_ref/doc/uid/TP40014097-CH2-ID1

关键字 in有什么作用?文档说使用从正文中分离参数并返回类型。我不确定我是否理解这一点。为什么不习惯于将 let result = 3 * number与 return result分开。

What does the keyword "in" do? The docs say to use "to separate the arguments and return type from the body". I'm not sure I understand this. Why isn't "in" used to separate "let result = 3 * number" from "return result".

推荐答案

A闭包只是一个函数,参数放在括号内,用关键词 in 分隔参数与函数体。以下两个示例定义了等效函数:

A closure is just a function with the parameters moved inside the brackets, with the keyword in to separate the parameters from the function body. The two following examples define equivalent functions:

func myFunc(number: Int) -> Int {
    let result = 3 * number
    return result
}

let myClosure = { (number: Int) -> Int in
    let result = 3 * number
    return result
}

实际上您可以用完全相同的方式调用它们:

You can actually call them both in exactly the same way:

let x = myFunc(2)       // x == 6
let y = myClosure(2)    // y == 6

注意第二个示例与第一个示例完全相同,仅在第一个示例中,参数(number:Int)-> Int 在方括号内,在第二个示例中,参数在方括号内,后跟关键字中。

Notice how the second example is exactly the same as the first, only in the first example, the parameters (number: Int) -> Int are outside the brackets, and in the second example the parameters are inside the brackets, followed by the keyword in.

map 的工作方式是采用数组(在您的示例中为数字)并创建一个新数组,该数组是对 numbers 中的每个元素应用闭包函数的结果。因此,如果数字 [1、2、3] ,则上面的示例将从开始1 。它将应用闭包函数,该函数将生成 3 (因为它所做的就是将第一个数组中的元素乘以3)。它会对个数字中的每个元素执行此操作,直到生成一个新数组 [3,6,9]

map works by taking an array (numbers, in your example) and creating a new array that is the result of applying the closure function to each element in numbers. So if numbers is [1, 2, 3], the example above will start with 1. It will apply the closure function which will produce a 3 (cuz all it does is multiply the element from the first array by 3). It does that for each element in numbers, until it has produced a new array, [3, 6, 9].

如果愿意,可以使用上述函数或上述闭包的名称调用 map ,或在 map 内部明确写出。以下所有示例都是完全等效的:

If you wanted to, you could call map using the names of either the above function or the above closure, or by writing it out explicitly inside of map. All of the below examples are totally equivalent:

let numbers = [1, 2, 3]

// Example 1
let times3 = numbers.map(myFunc)        // times3 == [3, 6, 9]

// Example 2
let timesThree = numbers.map(myClosure) // timesThree == [3, 6, 9]

// Example 3
let xThree = numbers.map({ (number: Int) -> Int in
    let result = 3 * number
    return result                       // xThree == [3, 6, 9]
})

请注意,示例3与示例2相同,仅在示例3中,闭包是在 map ,而在示例2中,已将闭包分配给名为 myClosure 的常量,并将该常量提供给 map

Note that Example 3 is the same as Example 2, only in Example 3 the closure is spelled out explicitly inside of map, whereas in Example 2 the closure has been assigned to a constant called myClosure, and the constant has been supplied to map.

希望这会有所帮助-闭合很有趣,但令人困惑。

Hope this helps - closures are fun, but confusing.

这篇关于不了解Swift中的闭包示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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