Swift Learning Notes (34)--function type __swift

Source: Internet
Author: User

(1) The function can also be assigned to a variable, what is the type of the variable at this time?

Import Foundation

func Add (a:int,b:int)-> int{return

  a+b
} Let

Anotheradd: (int,int)-> Int = add
Anotheradd (3,4)

(2) If the return value of the parameter is NULL, then how should this function type be written?

Import Foundation

func Add (a:int,b:int) {

  var c = a + b
} let

Anotheradd: (int,int)-> () = Add
anot Heradd (3,4)

Or:

Import Foundation

func Add (a:int,b:int)-> () {

  var c = a + b
} let

Anotheradd: (int,int)-> () = Add
anotheradd (3,4)

Or:

Import Foundation

func Add (a:int,b:int)-> Void {

  var c = a + b
} let

Anotheradd: (int,int)-> () = Add
anotheradd (3,4)

Or:

Import Foundation

func Add (a:int,b:int)-> void {

  var c = a + b
} let

Anotheradd: (int,int)-> void = Add
anotheradd (3,4)

(3) If this function has no parameters, what is the function type?

Import Foundation

func Add () {

} let

Anotheradd: ()-> Void = add
anotheradd ()

(4) Sorting

Import Foundation

var arr = [int] () for
_ in 1...20{

  arr.append (Int (Arc4random ()%100))
}

println (arr)

sorted (arr)
println (sorted (arr))

This is from small to large sort, if I want to from big to small, how should do it.

Import Foundation

var arr = [int] () for
_ in 1...20{

  arr.append (Int (Arc4random ()%100))
}

println ( ARR)

sorted (arr)
println (sorted (arr))

func comparetwoints (a:int,b:int)-> bool{return

  a > B
}

println (sorted (arr,comparetwoints))



GitHub home: https://github.com/chenyufeng1991. You are welcome to visit.



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.