What does $0 and $1 mean in Swift Closures?

2019-03-08 15:28发布

问题:

let sortedNumbers = numbers.sort { $0 > $1 }
print(sortedNumbers)

Can anyone explain, what $0 and $1 means in swift?

More Sample

array.forEach {
    actions.append($0)
}

回答1:

$0 is the first parameter passed into the closure. $1 is the second parameter, etc. That closure you showed is shorthand for:

let sortedNumbers = numbers.sort { (firstObject, secondObject) in 
    return firstObject > secondObject
}


回答2:

It represents shorthanded arguments sent into a closure, this example breaks it down:

Swift 4:

var add = { (arg1: Int, arg2: Int) -> Int in
    return arg1 + arg2
}
add = { (arg1, arg2) -> Int in
    return arg1 + arg2
}
add = { arg1, arg2 in
    arg1 + arg2
}
add = {
    $0 + $1
}

let result = add(20, 20) // 40


回答3:

The refer to the first and second arguments of sort. Here, sort compares 2 elements and order them. You can look up Swift official documentation for more info:

Swift automatically provides shorthand argument names to inline closures, which can be used to refer to the values of the closure’s arguments by the names $0, $1, $2, and so on.



回答4:

Swift 4.2

In your example $0 and $1 are Closure’s first and second String's arguments in a Shorthand Argument Names. The shorthand argument names are automatically provided by Swift. The first argument can be referenced by $0, the second argument can be referenced by $1, the third one by $2, and so on.

As you know, a Closure that's very close to a Lambda Function, or a Small Anonymous Function, is a self-contained block of functionality that can be passed around and used in your code. Closure has different names in other programming languages as well as slight differences in meaning – it's Lambda in Python and Kotlin or Block in C and Objective-C.

Let's see how Closure works using Shorthand Argument Names:

FIRST EXAMPLE :

let coffee: [String] = ["Cappuccino", "Espresso", "Latte", "Ristretto"]

1. Normal Function

func backward(_ n1: String, _ n2: String) -> Bool {
    return n1 > n2
}
var reverseOrder = coffee.sorted(by: backward)


/* RESULT: ["Ristretto", "Latte", "Espresso", "Cappuccino"] */

2. Closure Expression

reverseOrder = coffee.sorted(by: { (n1: String, n2: String) -> Bool in
    return n1 > n2
})

3. Inline Closure Expression

reverseOrder = coffee.sorted(by: { (n1: String, n2: String) -> Bool in return n1 > n2 } )

4. Inferring Type From Context

reverseOrder = coffee.sorted(by: { n1, n2 in return n1 > n2 } )

5. Implicit Returns from Single-Expression Closures

reverseOrder = coffee.sorted(by: { n1, n2 in n1 > n2 } )

6. Shorthand Argument Names

reverseOrder = coffee.sorted(by: { $0 > $1 } )

/* $0 and $1 are closure’s first and second String arguments. */

7. Operator Methods

reverseOrder = coffee.sorted(by: >)

/* RESULT: ["Ristretto", "Latte", "Espresso", "Cappuccino"] */

SECOND EXAMPLE :

let companies = ["bmw", "kfc", "ibm", "htc"]

Without Shorthand Argument Names:

let uppercasedCompanies = companies.map { (item) -> String in
    item.uppercased()
}

/* RESULT: ["BMW", "KFC", "IBM", "HTC"] */

With Shorthand Argument Names:

let uppercasedCompanies = companies.map {
    $0.uppercased()
}

/* RESULT: ["BMW", "KFC", "IBM", "HTC"] */

THIRD EXAMPLE :

Body of this closure is short and easy to understand:

let numbers: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

let filteredNumbers = numbers.filter { ($0 % 2) == 0 }

print(filteredNumbers)

/* RESULT: [2, 4, 6, 8, 10] */

Hope this helps.



回答5:

It is shorthand argument names.

Swift automatically provides shorthand argument names to inline closures, which can be used to refer to the values of the closure’s arguments by the names $0, $1, $2, and so on.

If you use these shorthand argument names within your closure expression, you can omit the closure’s argument list from its definition, and the number and type of the shorthand argument names will be inferred from the expected function type. The in keyword can also be omitted, because the closure expression is made up entirely of its body:

    reversed = names.sort( { $0 > $1 } )

Here, $0 and $1 refer to the closure’s first and second String arguments.