加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift 笔记(六)

发布时间:2020-12-14 07:14:39 所属栏目:百科 来源:网络整理
导读:我的主力博客:半亩方塘 Closures 1、Closures are functions without names . Here's a declaration for a varible that can hold a closure: var mutiplyClosure: (Int,Int) - Int You assign a closure to a variable like so: multiplyClosure = { (a: I

我的主力博客:半亩方塘


Closures


1、Closures arefunctions without names. Here's a declaration for a varible that can hold a closure:

var mutiplyClosure: (Int,Int) -> Int  

You assign a closure to a variable like so:

multiplyClosure = { (a: Int,b: Int) -> Int in 
    return a * b
}  

With your closure variable defined,you can use it just as if it were a function,like so:

let result = multiplyClosure(4,2)

As you'd expect,equals 8.

There are many ways to shorten their syntax:

multiplyClosure = { (a: Int,b: Int) -> Int in
    a * b
}  

multiplyClosure = { (a: Int,b: Int) in
    a * b
}

multiplyClosure = { (a,b) in  
    a * b
}  

multiplyClosure = {
    $0 * $1
}  

2、Consider the following code:
func operateOnNumbers(a: Int,_ b: Int,operation: (Int,Int) -> Int) -> Int {
    let result = operation(a,b)
    print(result)
    return result
}  
You can then use with a closure,like so:
let addClosure = { (a: Int,b: Int) -> Int in
    a + b
}
operateOnNumbers(4,2,operation: addClosure)  

You candefine the closure inlinewith thefunction call,like this:

operateOnNumbers(4,operation: { (a: Int,b: Int) -> Int in
    return a + b
})  

You can reduce the above to the following:

operateOnNumbers(4,operation: {
    $0 + $1
})  
There's one more way you can simplify the syntax,but it can only be done when the closure is the final parameter passed to a function . In this case,you can move the closure outside of the function call:

operateOnNumbers(4,2) {
    $0 + $1
}  

This is calledtrailing closure syntax.

3、The fact that closures can be used to capture variables from the enclosing scope can be extremely useful.

func countingClosure() -> (() -> Int) {
    var counter = 0
    let incrementCounter: () -> Int = {
        return counter++
    }
    return incrementCounter
}  

The closure returned from this function will increment its internal counter each time it is called. Each time you call this function you get a different counter.

For example,this could be used like so:

let counter1 = countingClosure()
let counter2 = countingClosure()

counter1()  // 0
counter2()  // 0
counter1()  // 1
counter1()  // 2
counter2()  // 1

The two counters created by the function are mutually exclusive and count independently. Neat!
resultoperateOnNumbersoperateOnNumbers

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读