(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.