常见的实现,
要熟悉了解,
至于闭包逃逸,
自动闭包这些内容,
可以以后用到时再学吧。
let names = ["Chris", "Alex", "Eva", "Barry", "Daniella"]
func backward(_ s1: String, _ s2: String) -> Bool {
return s1 < s2
}
var rNames = names.sorted(by: backward)
rNames = names.sorted(by: {(s1: String, s2: String) -> Bool in
return s1 > s2
})
rNames = names.sorted(by: {(s1: String, s2: String) -> Bool in return s1 > s2})
rNames = names.sorted(by: {s1, s2 in return s1 > s2})
rNames = names.sorted(by: {s1, s2 in s1 > s2})
rNames = names.sorted(by: {$0 > $1})
rNames = names.sorted(by: >)
rNames = names.sorted(){$0 > $1}
rNames = names.sorted{$0 > $1}
print(rNames)
let digitNames = [
0: "Zero", 1: "One", 2: "Two", 3: "Three", 4: "Four",
5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine"
]
let numbers = [16, 58, 510]
let strings = numbers.map {
(number) -> String in
var number = number
var output = ""
repeat {
output = digitNames[number % 10]! + output
number /= 10
} while number > 0
return output
}
print(strings)
func makeIncrementer(forIncrement amount: Int) -> () -> Int {
var runningTotal = 0
func incrementer() -> Int {
runningTotal += amount
return runningTotal
}
return incrementer
}
let incrementByTen = makeIncrementer(forIncrement: 10)
print(incrementByTen())
print(incrementByTen())
print(incrementByTen())
print(incrementByTen())