Swift 5.2 中引入的 callAsFunction
雀鹃,它可以讓我們直接以“調(diào)用實(shí)例”的方式 call 一個(gè)方法。使用起來很簡(jiǎn)單,只需要?jiǎng)?chuàng)建一個(gè)名稱為 callAsFunction
的實(shí)例方法就可以了:
struct Adder {
var base: Int
func callAsFunction(_ x: Int) -> Int {
return base + x
}
}
let add3 = Adder(base: 3)
add3(10) // => 13