aas*_*sya 56 arrays closures ios swift swift3
let sortedNumbers = numbers.sort { $0 > $1 }
print(sortedNumbers)
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释,快速的是什么$0
和$1
意味着什么?
更多样本
array.forEach {
actions.append($0)
}
Run Code Online (Sandbox Code Playgroud)
Ada*_*o13 89
$0
是传递给闭包的第一个参数.$1
是第二个参数,等.你显示的闭包是简写:
let sortedNumbers = numbers.sort { (firstObject, secondObject) in
return firstObject > secondObject
}
Run Code Online (Sandbox Code Playgroud)
Bob*_*bby 31
它表示发送到闭包中的缺少参数,这个例子将其分解:
斯威夫特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
Run Code Online (Sandbox Code Playgroud)
ARG*_*Geo 14
在你的榜样
$0
,并$1
在封闭的第一和第二串的论点中Shorthand Argument Names
。速记参数名称由Swift自动提供。第一个参数可以由引用$0
,第二个参数可以由引用$1
,第三个参数可以由引用$2
,依此类推。
如您所知,与Lambda Function或Small Anonymous Function非常接近的Closure是一个自包含的功能块,可以在代码中传递和使用。关闭有意味其他编程语言,以及细微的差别不同的名字-这是LAMBDA中的Python和科特林或阻止在ç和Objective-C中。
Shorthand Argument Names
:第一个例子:
let coffee: [String] = ["Cappuccino", "Espresso", "Latte", "Ristretto"]
Run Code Online (Sandbox Code Playgroud)
func backward(_ n1: String, _ n2: String) -> Bool {
return n1 > n2
}
var reverseOrder = coffee.sorted(by: backward)
/* RESULT: ["Ristretto", "Latte", "Espresso", "Cappuccino"] */
Run Code Online (Sandbox Code Playgroud)
reverseOrder = coffee.sorted(by: { (n1: String, n2: String) -> Bool in return n1 > n2 } )
Run Code Online (Sandbox Code Playgroud)
reverseOrder = coffee.sorted(by: { n1, n2 in return n1 > n2 } )
Run Code Online (Sandbox Code Playgroud)
reverseOrder = coffee.sorted(by: { n1, n2 in n1 > n2 } )
Run Code Online (Sandbox Code Playgroud)
reverseOrder = coffee.sorted(by: { $0 > $1 } )
/* $0 and $1 are closure’s first and second String arguments. */
Run Code Online (Sandbox Code Playgroud)
reverseOrder = coffee.sorted(by: >)
/* RESULT: ["Ristretto", "Latte", "Espresso", "Cappuccino"] */
Run Code Online (Sandbox Code Playgroud)
第二个例子:
let companies = ["bmw", "kfc", "ibm", "htc"]
Run Code Online (Sandbox Code Playgroud)
let uppercasedCompanies = companies.map { (item) -> String in item.uppercased() }
/* RESULT: ["BMW", "KFC", "IBM", "HTC"] */
Run Code Online (Sandbox Code Playgroud)
let uppercasedCompanies = companies.map { $0.uppercased() }
/* RESULT: ["BMW", "KFC", "IBM", "HTC"] */
Run Code Online (Sandbox Code Playgroud)
第三个例子:
filter
: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] */
Run Code Online (Sandbox Code Playgroud)
第四例:
$0
为立方度重复简写参数名称:let cubedNumber = { $0 * $0 * $0 } (25)
print(cubedNumber)
/* RESULT: 25^3 = 15625 */
Run Code Online (Sandbox Code Playgroud)
第五例:
$0
,:$1
$2
let math: (Int8, Int8, Int8) -> Int8 = { $0 + $1 - $2 }
func feedClosure() -> (Int8, Int8, Int8) -> Int8 {
return math
}
feedClosure()(10, 20, 100)
/* RESULT: (10 + 20 - 100) = -70 */
Run Code Online (Sandbox Code Playgroud)
第六个例子:
$0
,$1
,$2
,:$3
$4
let factorial = { $0 * $1 * $2 * $3 * $4 } (1, 2, 3, 4, 5)
print(factorial)
/* RESULT: 5! = 120 */
Run Code Online (Sandbox Code Playgroud)
第七个例子 – Swift vs Kotlin vs Python:
let element: [String] = ["Argentum","Aurum","Platinum"]
let characterCount = element.map { $0.count }
print(characterCount)
/* RESULT: [8, 5, 8] */
Run Code Online (Sandbox Code Playgroud)
通常Kotlin的lambda表达式只有一个带有隐式名称的参数:it
。
val element = listOf("Argentum","Aurum","Platinum")
val characterCount = element.map { it.length }
println(characterCount)
/* RESULT: [8, 5, 8] */
Run Code Online (Sandbox Code Playgroud)
But in Python there's no equivalent of Shorthand Argument Name
。
element = ["Argentum","Aurum","Platinum"]
characterCount = list(map(lambda x: len(x), element))
print(characterCount)
/* RESULT: [8, 5, 8] */
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
30889 次 |
最近记录: |