Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does $0.1, $0.0, etc. mean in Swift?

Tags:

swift

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?

like image 223
Leem.fin Avatar asked Sep 28 '17 13:09

Leem.fin


1 Answers

$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

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

like image 50
mag_zbc Avatar answered Oct 09 '22 19:10

mag_zbc