A Swift Tour (3)

Source: Internet
Author: User

Functions and Closures

Use func to declare a function, call the function by using the parameter list of parentheses, and use --> to separate the return type, parameter name, and type of the function. For example:

Func greet (name: String, day: String)-> String {return "Hello \ (name), today is \ (day ). "} greet (" Bob ", day:" Tuesday ") // This is the call method in the swift document, but I always report an error when writing it in xcode6, so the following greet ("Bob", day: "Tuesday") // This method will not be wrong.

Using a single tuple, a function can return multiple values.

func getGasPrices() -> (Double,Double,Double)    {        return (3.97,3.59,3.79)    }getGasPrices()

I don't know how to use the above method to receive the returned value.

The func parameters are also variable. You can put multiple parameters in an array.

func sumOf(sumbers:Int...) -> Int    {        var sum = 0        for number in sumbers        {            sum += number        }        return sum    }println(sumOf())    //return 0println(sumOf(42, 597, 12))   //return 651

Functions can be nested. nested functions can access variables declared in external functions. You can use nested functions to solve complicated logic:

    func returnFifteen() -> Int    {        var y = 10        func add()        {            y += 5        }        add()        return y;    }        println(returnFifteen()) //return 15

The function is of the first-class type, which means that the return value of the function can be another function:

Func makeIncrementer ()-> (Int-> Int) {func addOne (number: Int)-> Int {return 1 + number} return addOne} var increment = makeIncrementer () increment (7) // the above Code always reports an error, do not know what the cause // error: Command/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault. xctoolchain/usr/bin/swift failed with exit code 254 // If any expert knows, please advise

A function can be used as another function as its parameter.

Func hasAnyMatches (list: Int [], condition: Int-> Bool)-> Bool {for item in list {if (condition (item )) {return true} return false} func lessThanTen (number: Int)-> Bool {return number <10;} var numbers = [20, 19,] let temp = hasAnyMatches (numbers, lessThanTen) println (temp) // This is the same as the above error, and I have a problem with xcode6.

You can use {} to create a closure.

numbers.map({    (number: Int) -> Int in    let result = 3 * number    return result    })

This closure can be written more concisely sometimes, for example, you know its return type or other

numbers.map({ number in 3 * number })
sort([1, 5, 3, 12, 2]) { $0 > $1 }

The closure above is not clear...

 

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.