什么$ 0.1,$ 0.0等在Swift中意味着什么?

时间:2022-05-11 15:45:27

I saw a dictionary data, its value is like:

我看了一个字典数据,其价值如下:

var myDict = ["name": "John",
              "age": 28]

I see a code like below:

我看到如下代码:

myDict.flatMap {
           let a = $0.0
           let b = $0.1
           ...
 }

What does $0.0 mean? What does $0.1 mean? What is the difference between $0 and $0.1?

$ 0.0是什么意思? 0.1美元是什么意思? $ 0和$ 0.1之间有什么区别?

1 个解决方案

#1


12  

$0 is a shorthand name for first argument passed to closure. In this case, as you're mapping a Dictionary, that argument is a tuple - hence $0.0 is a key, and $0.1 is a value

$ 0是传递给closure的第一个参数的简写名称。在这种情况下,当您映射一个Dictionary时,该参数是一个元组 - 因此$ 0.0是一个键,$ 0.1是一个值

For more info on shorthand argument names, see Swift documentation on closures

有关简写参数名称的更多信息,请参阅关闭闭包的Swift文档

#1


12  

$0 is a shorthand name for first argument passed to closure. In this case, as you're mapping a Dictionary, that argument is a tuple - hence $0.0 is a key, and $0.1 is a value

$ 0是传递给closure的第一个参数的简写名称。在这种情况下,当您映射一个Dictionary时,该参数是一个元组 - 因此$ 0.0是一个键,$ 0.1是一个值

For more info on shorthand argument names, see Swift documentation on closures

有关简写参数名称的更多信息,请参阅关闭闭包的Swift文档