Swift Learning Tip (3) Closures per day

Source: Internet
Author: User
Tags rowcount

Closure Introduction
最近在学习swift的过程中,发现很多人都对于swift的闭包有很深的疑惑,今天就以我个人学习swift 1.2版本下的一些经验进行一些分享,如有错漏,望大家指出。ps.《关于Swift学习之每日一tip》我这段时间会持续更新,而其中关于swift的内容都是在今年(2014)春节之后,苹果推出Xcode6.3测试版的之后推出的swift 1.2的语法,关于1.2与1.1的语法,我会在明天的内容中进行一下介绍。
Universal closures

First, write a common closure code for everyone.

demo{ (name) -> () in    println("\(name): 完成了吗?")}

The above closure is actually a callback for the closure, and the contents of the prepared closure are written below

func demo(finished:(name: String)->()) {    println("小明正在玩命工作中... ")    // 执行闭包,完成耗时操作后,做回调    let t = "大 BOSS"    finished(name: t)}

Execution Result:

小明正在玩命工作中... 大 BOSS: 完成了吗?

The above is one of the most commonly used closures, where closures are actually very similar to the block definition in Objective-c, which is to define the contents of the closed packets that are prepared later and then make the closure calls where we need them.
After we understand the principle of closure, we will analyze the closure code.

// 闭包的格式    /**    { (参数) -> (返回值) in          代码实现    }    */

When you are learning to close a packet, you can look at the call of the closure in this format, because the closure is more intuitive when called.

    • In the above code, in the callback, we print a word, passing in a closed package within the defined name.
    • In the preparation of the closures, we can see that, here, it can actually be seen as a function in C + + or other high-level languages, and our closures are actually a parameter.
    • Inside this function, we make a call to the closure where we need it.
    • We then make the callback code where we need to execute the function, passing in the effect we want to get at the callback.
    • The whole process is actually the design idea of the proxy design pattern, which is very common in Apple's iOS development, which can refer to the agent and block in Objective-c.

Do not know through the above content, we have a preliminary understanding of closures?

Here is a common simplified notation for closures:

Trailing closures

In fact, the first time I saw a trailing closure, I didn't realize it was a closure, because he was too simple and simple to what extent?

demo2 {    println("over"        }

Yes, you read it right, that's all. This is a call to a trailing closure.
In fact, his full version should look like this:

demo2 { () -> () in      println("over")}

See here, believe that the smart classmate has guessed out, this trailing closure as the name implies, if the closure is the last parameter, can be simplified, when the closure of the parameters are not even, we can write like the above-mentioned simple closure call.
Here we define this closure:

func demo2(completion:()->()) {    // 执行耗时操作    println("耗时操作")    // 回调    completion()}

This closure is exactly what I mentioned above. The closure is the last parameter, and the closed packet has no parameters.
Of course, the trailing closure only need to be closed packet is the last parameter of the case can be abbreviated, here is not written, because I do not recommend that way of writing, readability is too poor!!

Finally, let me briefly say the return value of the closure:

The return value of the closure

Nonsense not much to say, on the code:

// 闭包的返回值    demo3 { () -> Int in        return 10    }

Here, everyone seems to have found, I write code when I like to write the call, and then write the contents of the closure, here is of course for everyone to understand, throw away the bondage of grammar, do not like to spray yo ~ ^_^.
The following is the definition of closures:

func demo3(rowCount:()->Int) {    // 通过闭包的返回值,获得数据信息    let count = rowCount()    println("行数 \(count)")    // ...}

So simple process, I believe that the small partners should not need me to more nonsense, it is here today, see you tomorrow.

Swift Learning Tip (3) Closures per day

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.