了解Swift中Map函数的速记闭合语法

了解Swift中Map函数的速记闭合语法

本文介绍了了解Swift中Map函数的速记闭合语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解map函数使用的一些速记语法。



以下是设置

  let array = [1、2、3] 

//这些有意义
let arr1 = array.map({ String($ 0)})
let arr2 = array.map {String($ 0)}
let arr3 = array.map({
中的数字返回String(number)
} )
let arr4 = array.map({(number)->
中的字符串String(number)
})

这里是造成混乱的地方。我可以很快放弃map的花括号,但这似乎是无法完成的,因为对于我自己的函数而言,我有一个尾随的闭包。也许正在做出一些神奇的推断?还有为什么用这种方式初始化String?

  //这没有意义。放弃大括号?我做不到!!! 
let arr5 = array.map(String.init)
let arr6 = array.map(String())//编译错误:无法将'String'类型的值转换为预期的参数类型'@noescape (Int)抛出-> _'

这是我试图使用与map相似的语法

  func crap(block:(Int)-> String){
print( Int to string block + block(1));
}
//显然可以用
废话{ \($ 0)一些垃圾}
//编译错误:闭包
中不包含匿名闭包参数crap( \($ 0)一些垃圾)


解决方案

将花括号()与花括号 {} 区别开来。



map 的括号中,放置了一个函数。可能是一个函数 reference (即函数的名称):

  let arr = [ 1,2,3] 
func double(i:Int)-> Int {return i * 2}
让arr2 = arr.map(double)

它可以是匿名函数,表示花括号中的函数体:

  let arr = [1,2,3] 
let arr2 = arr.map({$ 0 * 2})

但是在 情况,并且仅在这种情况下,您可以(作为快捷方式)使用跟踪闭包语法:

 让arr = [1,2,3] 
让arr2 = arr.map(){$ 0 * 2}

但是由于 map 不带其他参数,因此您可以省略括号,这是Swift中唯一的情况您可以调用不带括号的函数:

  let arr = [1,2,3] 
let arr2 = arr .map {$ 0 * 2}


I'm trying to understand some of the short hand syntax used by the map function.

The following is the setup

    let array = [1, 2, 3]

    // these make sense
    let arr1 = array.map({String($0)})
    let arr2 = array.map{String($0)}
    let arr3 = array.map({ number in
        return String(number)
    })
    let arr4 = array.map({ (number) -> String in
        String(number)
    })

Here is where the confusion lays. In swift I can forgo the curly braces for map, but this seems like something that can't be done, for my own functions where I have a trailing closure. Some magical inference that's being made perhaps? Also why is the String initialized in this way?

    // this doesn't make sense. Foregoing the curly braces? I can't do that!!!
    let arr5 = array.map(String.init)
    let arr6 = array.map(String())    // Compile Error: Cannot convert value of type 'String' to expected argument type '@noescape (Int) throws -> _'

This is me trying to use similar syntax as map

func crap(block:(Int)-> String) {
    print("Int to string block" + block(1));
}
// works obviously
crap{ "\($0) some garbage" }
// compile error : Anonymous closure argument not contained in a closure
crap( "\($0) some garbage" )
解决方案

Distinguish parentheses () from curly braces {}.

In a sense, only the parentheses version is "real", because, after all, that is what a function call requires. In the parentheses when you call map, you put a function. It may be a function reference (i.e. the name of a function):

    let arr = [1,2,3]
    func double(i:Int) -> Int {return i*2}
    let arr2 = arr.map(double)

Or it can be an anonymous function, meaning a function body in curly braces:

    let arr = [1,2,3]
    let arr2 = arr.map({$0*2})

But in that case, and that case only, you can (as a shortcut) use the "trailing closure" syntax instead:

    let arr = [1,2,3]
    let arr2 = arr.map(){$0*2}

But since map takes no other parameters, you can then also omit the parentheses — the only situation in Swift where you can call a function without parentheses:

    let arr = [1,2,3]
    let arr2 = arr.map{$0*2}

这篇关于了解Swift中Map函数的速记闭合语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 07:27