Swift starts learning _04 from scratch

Source: Internet
Author: User

Swift Fourth day

On Friday a little thing, no time to study, Saturday Sunday and busy to find a house, have no time to see, there are times must insist to look!

The main function is also some knowledge:

The ①.//variable has the same parameter type as the function, which can be directly assigned

Func Makeplus (A:int, B:int),int{    return A + B}var replacefun: (int, int)->int = The Makeplus// variable has the same parameter type as the function and is a println that can be directly assigned ("Result: \ (Replacefun (2,3))" )

Ii.

function nesting to determine which function to execute according to the passed BOOL value

//function nesting to determine which function to execute according to the passed BOOL valueFunc Returnbool (Onreturn:bool, Testinput:int)int{func boolisyes (input:int)-int{returnInput +1} func Boolisno (input:int)-int{returnInput-1    }    returnOnReturn?Boolisyes (testinput): Boolisno (testinput)}var x=2Let y= Returnbool (x >0,5)//(Int)->int means that the return parameter is shaped, and the return value is a shaped function

③.//sort function

Sort (&names, backwards)// sort function, here the second parameter needs to pass in a parameter is a string, the return value is bool function println (names)

④.//Closed-Packet expression

// {(parameters)-ReturnType in statements} // The order corresponds to the closure function, and the function body part of the closure is introduced by the keyword in. The keyword indicates that the closure parameter and the return value type definition have been completed and the closure function body is about to begin.  inreturn s1 > s2}) println (names)

⑤.

// The type is inferred from context. Because of Swift's powerful inference ability, this closure function can also be written, and he automatically infers the data type based on the context. inch  // There is a problem here, the use of ' > ' will be error println (names)

⑥.

// parameter name abbreviation sort (&names, {$0 < $1})////// also use ' > ' here will be an error, currently not known cause println (names)

⑦.

// operator function, Swift's string type defines an implementation of the strings for the greater-than sign (>), which accepts two string arguments as a function and returns a value of type bool, which can also be replaced by ' < ', where there is no error. sort (&names, >) println (names)

⑧.

// trailing closures. If you need to pass a long closure expression as the last argument to a function, you can use a trailing closure to enhance the readability of the function. A trailing closure is a closure expression that is written after the function bracket, and the function supports calling it as the last parameter, func funcuseclosure () {    }sort (&names) {    $  0 < $1}

For the concept of closure, really I do not understand what he means, because before the OC, for these closures do not understand, ready to take a good look at this thing, today for the moment to see so much, to understand the next closure of this thing to go ...

Swift starts learning _04 from scratch

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.