Swift in-depth understanding closures

Source: Internet
Author: User

We can use Swift's closure to define the value of the variable. Let's start with a simple example of how you feel.

A method for defining a variable of a string:

Direct assignment:

var string " A String "

You can also define it in a closure way:

var string1:string = {    return"A String"} () 

Closures can also be defined so that the equals sign and parentheses are omitted:

var string2:string {    return"A String"}

The Get method can be defined in closures

var string3:string {    get  {        return'A String "     }}

You can also define a set method, but you must have a GET method to define a set method

var string4:string {    get  {        return'A String'     }    set  {        print ("set OK")    }}

You can also use the Willset and Didset methods, noting that Willset/didset cannot be used in conjunction with Get/set, and that variables need to have an initial value when using Willset/didset

var " A String " {    Willset {        print ("newvalue: \ (newvalue)") //"newvalue:new Value\n "    }    didset {        print ("oldValue: \ (oldValue)" ) //"Oldvalue:a string\n"    }}

String5 = "New Value"

From the above example, we feel that the expression of closure is implemented in many forms. The following system summarizes.

The most fully defined form of closures is:

{

(arguments)->returntype in

Code

} (arguments)

You can define parameters in closures and return values. The closure is followed by parentheses, and the arguments are passed in parentheses. For example:

var componsestring = {        in            return arg1 + arg2    } ("  A""B") print (componsestring) //" ab\n " 

Based on the most complete definition of the above, I can omit the type of the parameter:

var componseString1 = {    in            return arg1 + arg2    } (" A " " B " ) print (componseString1)    //"ab\n"

Why can I omit parameter types? That is because Swift's type derivation automatically determines the type of the parameter according to the arguments of the following parentheses.

Then we can omit the return value type from the closure

var componsestring2:string = {    in            return arg1 + arg2} ("  A""B") print (componseString2)  //" ab\n "     

Note that after the closure omits the return value type, the variable will display the type that declares it, and the return value type can be omitted because swift type derivation knows the type of the variable first, so you can omit the return value type.

You can also omit the parameters.

var componsestring3:string = {        return $0 + $1    } ("  A""B") print (COMPONSESTRING3)  //" ab\n "    

If there is only one line of code in the closure, return can be omitted.

var componsestring4:string = {    $0 + $1    } ("A " " B " ) print (COMPONSESTRING4)  //"ab\n"  

If the closure does not have a parameter defined, like this

var componsestring5:string = {    return"A String"     } () print (COMPONSESTRING5)        //"A String"

You can omit the parentheses, omit the parentheses, and not write the equals sign.

var componsestring6:string {        return"A String"     }print (componseString6)        //"A String"

The above-mentioned closure method is familiar, if you have learned Swift's class, know that the properties of the class can be expressed in closures, you can define the set, get, Willset,didset and other methods in the closure. So can the above variables be defined as well?

var string4:string {    get  {        return'A String'     }    set  {        print ("set OK")    }}
var string5:string = "A String" {    willset {        print ("NewValue: \ (newvalue)") //"NE Wvalue:new value\n "    }    didset {        print (" OldValue: \ (oldValue) ") //" oldvalue:a string\n "    }}  

String5 = "New Value"

In fact, the property of a class is a variable. The properties of a class can be defined by a set, a get, or a willset,didset that can be defined in a variable.

This digest from: http://my.oschina.net/lanrenbar/blog/467768

Swift in-depth understanding closures

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.