The functions of swift and function pointers, closure closure, and other related contents are introduced

Source: Internet
Author: User

Tag:inter   failed    function nesting    get   pop    one    address    div   and   

<span style= "FONT-SIZE:24PX;" >//function//demo1 no parameter type func testconcat () {println ("test function");} Testconcat ()//Call Demo1//demo2 Multi-parameter, a return value/* function begins with Func, representing a function v1 representing the number of parameters. String parameter type, return value type String*/func testconcats (v1:string,v2:string)->string{return "\ (v1) and \ (v2)";} function parameters include the label func Testconcatandmark (V1:string,andvalue v2:string,andnumber v3:string)->string{return "\ (v1), \ (v2),    \ (v3) "}//test function func testfunction () {Let V = testconcats (" Hello "," World ");    println (v);    Let V2 = Testconcatandmark ("David", Andvalue: "Dream", Andnumber: "good"); println (v2)}testfunction ()//function returns multivalued func GetInfo (), (String,string,int) {return ("David", "Dream", 100);} Let (V1,v2,v3) =getinfo ();p rintln ("v1=\ (v1), V2=\ (v2), v3=\ (v3)");//function Nesting function func testfunctioninfunction ()->int{var    y = 10;    Func Add () {y+=5;    } add (); return y;} Let V6 = Testfunctioninfunction ();//*******************************//function pointer * * Five-Star Difficulty * * The return value is (Int->int). It's a function address. The first int is the parameter type, and the second int is the return value */fUNC Getfuncpointer (), (int->int) {func addone (num:int)->int{return num+1; Return addone;//returns a function address}var funpointer = Getfuncpointer ();//funpointer is a function pointer var v7 = funpointer (100);//function called Func        Get () (int->string) {func post (count:int)->string{if count>50{return "success"; }else{return "Failed"}} return post;} Let V8 = Get ();//v8 gets the address of the get () function Let V9 = V8 ();//v9 gets the return value of the Get function//function pointer/address as a parameter, the function is passed as a parameter to the function. Transfer of functions </span>
<span style= "FONT-SIZE:24PX;" ></span>
<span style= "FONT-SIZE:24PX;" >//closure is a difficult problem, the function of closure is mainly the communication between functions and functions, a bit like block</span>
<span style= "FONT-SIZE:24PX;" ></span><pre name= "code" class= "OBJC" >//(num:int)->bool closure parameter type func hascolsurematch (arr:Int[], VALUE:INT,CB: (num:int)->bool) {for    item in arr{        if CB (item) {            return true;        }    }    return false;} var ar = [20,9.100,34,89,39];var v1 = Hasclosurematch (arr,40,{    (num:int), Bool in    return num >= 40;}); println ("V1 is \ (v1)");



Swift's functions and function pointers, closures closure, and so on

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.