ios-swift-functions and closures

Source: Internet
Author: User

First, the function

Keyword: func

Parameter list, split

Split parameters and return values using

1     Func greet (person:string, day:string), String {2         return"Hello \ ( person), today is \ (day). " 3     }4     "Bob""Tuesday" )

Precede the parameter name with _ to hide the parameter label, or you can add the parameter name.

1     Func Greet (_ Person:string, on day:string), String {2         return"Hello \ ( person), today is \ (day). " 3     }4     greet ("John""Wednesday ")

Use tuples (tuple) to build composite type values. For example, a function returns multiple values. The elements of a tuple can be obtained by name or subscript.

1Func calculatestatistics (scores: [Int])(Min:int, Max:int, sum:int) {2         varMin = scores[0]3         varmax = scores[0]4         varsum =05         6          forScoreinchscores {7             ifScore >Max {8Max =score9}Else ifScore <min {TenMin =score One             } ASum + =score -         } -          the         return(min, max, sum) -     } -Let statistics = calculatestatistics (scores: [5,3, -,3,9]) - print (statistics.sum) +Print (statistics.2)

The function can use the variable parameters, which will be put into the array.

1Func sumof (Numbers:int ...)Int {2         varsum =03          forNumberinchNumbers {4Sum + = Number5         }6         returnsum7     }8 sumof ()9Sumof (Numbers: the,597, A)

Functions can be nested inside a function.

1     Func Returnfifteen ()- Int {2         var3        func Add () { 4             5 5         }6        Add ()7         return  y8      }9     Returnfifteen ()

A function can be a type of another function

1     Func Makeincrementer (), (int), int) {2         func addone (number:int), int {  3             return1 + number4        } 5          return  addone6    }7     var increment =  Makeincrementer ()8     Increment (7)

function can be used as a parameter to another function

1Func hasanymatches (list: [int], condition: (int)-Bool)Bool {2          forIteminchList {3             ifcondition (item) {4                 return true5             }6         }7         return false8     }9Func Lessthanten (Number:int)Bool {Ten         returnNumber <Ten One     } A     varnumbers = [ -, +,7, A] -Hasanymatches (List:numbers, Condition:lessthanten)

The closure (closures) function is a special case of closure: code block delay call.

You can write a closure with curly braces {}. Use "in" to distinguish the closure parameters and the return values from the closure body.

    In 3* number return  result    })                 

Can be simpler and functionally the same.

    inch 3 * Number })    print (mappednumbers)

You can use a numeric subscript instead of a name access parameter, which is useful for short closures.

When a closure is the only parameter of a function, parentheses can be omitted.

    Let Sortednumbers = numbers.sorted {$0 > $1  }    print (sortednumbers)

Well, try!

ios-swift-functions and 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.