Closures

作者: 宋奕Ekis | 来源:发表于2021-08-02 17:21 被阅读0次

Closure Expressions

The Sorted Method

let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
func backward(_ s1: String, _ s2: String) -> Bool {
    return s1 > s2
}
var reversedNames = names.sorted(by: backward)
// reversedNames is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]

the above example shows that we use a backwardfunction as a value to be sent into the sorted function.

but this is written as a closure ordinarily.

reversedNames = names.sorted(by: { (s1: String, s2: String) -> Bool in
    return s1 > s2
})

We can see the closures structure:

{ (parameters) -> return type in
statements
}

Inferring Type From Context

At the above example, the sortedfunction always be called on a strings array, so its argument must be a function of type (String, String) -> Bool, so we don't need to written the whole form.

It can be written as below:

reversedNames = names.sorted(by: { s1, s2 in return s1 > s2 } )

Implicit Returns from Single-Expression Closures

And if there only be a single expression in closures, we can omit the return as well.

reversedNames = names.sorted(by: { s1, s2 in s1 > s2 } )

Shorthand Argument Names

Simpler and simpler, as below:

reversedNames = names.sorted(by: { $0 > $1 } )

Operator Methods

Simplest!!!

reversedNames = names.sorted(by: > }

This > can be related to a function, actually, it dose a function.

Capturing Values

Warning!!!

Watch out this expression!

func makeIncrementer(forIncrement amount: Int) -> () -> Int {
    var runningTotal = 0
    func incrementer() -> Int {
        runningTotal += amount
        return runningTotal
    }
    return incrementer
}
let incrementByTen = makeIncrementer(forIncrement: 10)
incrementByTen()
// returns a value of 10
incrementByTen()
// returns a value of 20
incrementByTen()
// returns a value of 30

let incrementBySeven = makeIncrementer(forIncrement: 7)
incrementBySeven()
// returns a value of 7

We can see the above example, due to the nested function incrementer capture the runningTotal, so runningTotal will remains even if incrementByTen finish, and incrementBySeven is not influenced.

Because the function type and closures type is reference types.

Same for closures, but it will may cause strong reference cycles, which we will learn how to solve.

Escaping Closures

If we need the closures excute after the function ends, we should use key word @escaping before the parameter’s type.

var completionHandlers: [() -> Void] = []
func someFunctionWithEscapingClosure(completionHandler: @escaping () -> Void) {
    completionHandlers.append(completionHandler)
}   

Let’s think!

相关文章

  • Swift工具包

    Closures[https://github.com/vhesener/Closures]为UIKit和Foun...

  • closures

    closures 是js的特色,它的本质即一个函数+这个函数生成的上下文环境。我们以下面的代码为例来讲解closu...

  • Closures

    广义上函数和内嵌函数都属于特殊的闭包 闭包的三种格式1-全局函数是有名字,但捕获值的闭包2-内嵌函数是有名字,可以...

  • closures

  • closures

    一个闭包例子,侵删

  • Closures

    Closure Expressions The Sorted Method the above example s...

  • Swift 2.1 Closure(闭包)

    # 闭包(closures) -/*闭包(Closures) - *闭包是自包含的功能代码块,可以在代码中使用或者...

  • <iOS 实践经验>判断循环引用的小技巧

    在 swift 中经常使用 Closures, 但有的时候使用 Closures 会造成一些不可避免的问题, 比如...

  • Swift闭包--简不简洁?!优不优雅?!

    闭包(Closures) 闭包是自包含的函数代码块,可以在代码中被传递和使用。 Closures are self...

  • Swift Closures随想

    本文不会对Closures的基本语法进行解读,如果对于Closures语法有所疑惑,请参考Apple官方文档本文主...

网友评论

    本文标题:Closures

    本文链接:https://www.haomeiwen.com/subject/vrjjvltx.html