On the function types and closures in Swift

Source: Internet
Author: User

Before we talk about Swift's function type, let's recall what we've learned about defining a swift function.

Func Add (a:int,b:int), Int {    return A + B}

Okay, let's start with the explanation of our function type.

The type of the above function is (int, int)->int

Using function types

As we all know, in swift, the function type is just like any other data type, which means we can assign a value to a function's constant or variable.

var F2: (int,int)-Int = addf2 (2,3)  // result 5

Well, let's take a look at the example of a function type comparison, first, as a function, we can pass it as a parameter to another function

First, we'll define an interface.

protocol Processint {    func guolv (data:int),Bool}

function Body Content

Func processArray2 (Chulizhe:processint),[Int] {    //To find an even number within an array
= [1,8,7,3] = [] for in array{ if chulizhe.guolv (item) { Result.append (item) } } return result}

Functions to be passed in as arguments

class oushu:processint {    func guolv (data:int),bool{        return20     }}

Calling functions

ProcessArray2 (Oushu ())    //  result 8

Well, the function type is introduced here, just down is the closure of the introduction ...

Closures: Functional self-contained modules that can be passed and used in code, and closures are reference types.

Closures can "understand" an object that is instantiated through the following class

1. Closures are an object that is a reference type

2. Closures are objects of a function type

The syntax of a closed-packet expression

{(parameter, parameter), return type in}

The following closure expression example uses the Sort function to ascending (descending) an array of type int, and the following is the initial array value:

Let array = [1,2,8,3,9]

The sort function has two parameters:

    • An array of known type values.
    • A closure that takes two parameters of the contents of the same type of array and returns a Boolean value that indicates whether the first value is placed before or after the second value at the time of sorting. If the first value should be preceded by a second value, the closure needs to return true, otherwise false is returned.
    • Func Compare (A:int,b:int), Bool {    return a < b}array.sort (Compare)    // [1,2,3,8,9,20]

This example sorts an array of type int, so the sort closure needs to be a function of type Bool (int, int).

Closure expressions, no additional defined functions required

The following methods can be used to achieve the effect of sorting

1. The effect of sorting can be achieved by calling Array.Sort ()

inch return a < b}) resu   //[1,2,3,8,9,20]inreturn a > b}) result2  //[20,9,8,3,2,1]

2. Inference by parameter type and return type

inch return a > b}) result2  //[9, 8, 3, 2, 1]

3. A single line of code automatically returns the result of this code

inch  a > b}) result4  //[9, 8, 3, 2, 1]

4 automatically provides a short parameter name $0,$1,$2 ....

Let Resu4 = Array.Sort ({$0>$1})  //[9, 8, 3, 2, 1]

5. If a function (sort), it has only one parameter, the type of this parameter is the function type, then you can put the contents of the curly braces outside the parentheses

Let RESULT5 = Array.Sort () {    $0 > $1}result2   //[20, 9, 8, 3 , 2, 1]

Capture

Closures can capture constants or variables in the context in which they are defined. Even if the original scope for defining these constants and variables no longer exists, the closures can still reference and modify these values in the closure function body.

Swift's simplest form of closure is a nested function, that is, a function defined in another function body. A nested function captures all parameters of its outer function, as well as the constants and variables defined.

Func funcfactory (),int {    0        , int {        1        return Total    }     return Innerfunc     = funcfactory () R1 ()   //1R1 ()  //2

Every time this function is called, Total will add 1, so the closure is here to end.

On the function types and closures in Swift

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.