Swift (lower) function closure structure

Source: Internet
Author: User

You need to define a function with a func keyword SayHello the function name in parentheses (parameter name: parameter type)--refers to the body of the representation function inside the return value {} curly Braces

Func SayHello (name:string, number:int)->string {

return name

}

SayHello ("DT", 99)

We can add a label (external parameter) to the function parameter to indicate what kind of argument we need to enter or what to do with it.

Func Saygoodbye (Peronname name:string, number:int)->string {

return name+ "\ (number)"

}

Saygoodbye (peronname: "XG", 99)

Mark

The parameter plus # indicates that the parameter name can be accessed both outside and inside of the

Func say1 (#name: String, #sex: string)->string {

Return name + "" + Sex

}

Say1 (Name: "lb", Sex: "?")

The parameter is preceded by a modifier that represents the default parameter, a default value is assigned, and if the argument is passed when the function is called, the default value is replaced, and if not passed, the default value is returned.

Func Say2 (name:string, _ sex:string = "!") ->string {

Return name + "" + Sex

}

Say2 ("SX", "?")

Say2 ("ZX")

When you are unsure of the number of arguments that need to be passed to the function, write the following ... The form

Func say3 (Numbers:int ...) ->int {

var sum:int = 0

For I in numbers {

sum + = i

}

return sum

}

Say3 (10, 20, 30)

If we want to change the value of the arguments passed in, we need to modify the InOut keyword in front of the parameter name and add the & symbol to the argument before the parameter is called.

var foo = 5

Func say4 (inout num:int) {

num = 10

}

Say4 (&foo)

Foo

Func say5 (name:string, sex:string), (name:string, sex:string) {

Return ("XM", "?")

}

Say5 ("Xq", "!")

Closed Package

Declaration: (parameter list), return value

Func Sortnum (Arr:[int], Num:int, CB: (Num1:int, Num2:int)->bool)->[int] {

var result = [Int] ()

For I in Arr {

If CB (NUM1:I, Num2:num) {

Result.append (i)

}

}

return result

}

Closure implementation: {(parameter list), return value in function body}

var numbers = [40, 50, 17, 54, 84, 23]

Sortnum (Numbers, Num1:int, num2:int)->bool in

return NUM1 > Num2

})

Sortnum (numbers, +, {(NUM1, num2), Bool in

return NUM1 > Num2

})

Sortnum (Numbers, $, {(NUM1, num2), Bool in

NUM1 > Num2

})

$ A is the No. 0 parameter in the closure parameter list, which represents the 1th parameter in the closure parameter list

Sortnum (Numbers, 33, {

$ > $

})

Sortnum (Numbers, >)

Trailing closures are written outside the function's argument list, representing the last parameter of the function

Sortnum (Numbers, one) {(NUM1, num2), Bool in

return NUM1 > Num2

}

A struct is a value type class that is a reference type (manipulating an object)

struct Student {

Construction method

Init (name:string, Age:int) {

Self.name = Name

Self.age = Age

}

Contains properties and methods

var name:string = "AA"

var age:int = 10

Func speak (Num1:int, num2:int)->int{

return NUM1 + num2

}

}

var stu = Student (name: "FF", age:99)

Stu.name

Stu.age

Stu.speak (3, Num2:2)

struct Point {

var x = 0

var y = 0

}

struct Size {

var width = 0

var height = 0

}

struct Myrect {

Store Properties

var origin = point ()

var size = size ()

var center:point{

Calculated properties

get{

var Originx = self.origin.x + SELF.SIZE.WIDTH/2

var originy = self.origin.y + SELF.SIZE.HEIGHT/2

Return point (X:originx, Y:originy)

}

Set (Newcenter) {

self.origin.x = NEWCENTER.X-SELF.SIZE.WIDTH/2

SELF.ORIGIN.Y = NEWCENTER.Y-SELF.SIZE.HEIGHT/2

}

}

}

var rect = Myrect (Origin:point (x:0, y:0), Size:size (width:100, height:200))

Class Person {

var name:string

var age:int?

Init (name:string, Age:int) {

Self.name = Name

Self.age = Age

}

destructor method

deinit{

}

Func say () {

println ("Say")

}

}

var p = person (name: "Xq", age:90)

P.name = "XX"

Swift (lower) function closure structure

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.