(1)函数也可以赋值给一个变量,此时这个变量的类型是什么呢?
import Foundation func add(a:Int,b:Int) -> Int{ return a+b } let anotherAdd:(Int,Int) -> Int = add anotherAdd(3,4)
import Foundation func add(a:Int,b:Int) { var c = a + b } let anotherAdd:(Int,Int) -> () = add anotherAdd(3,4)
import Foundation func add(a:Int,b:Int) -> () { var c = a + b } let anotherAdd:(Int,Int) -> () = add anotherAdd(3,4)
import Foundation func add(a:Int,b:Int) -> Void { var c = a + b } let anotherAdd:(Int,Int) -> () = add anotherAdd(3,4)
或者:
import Foundation func add(a:Int,b:Int) -> Void { var c = a + b } let anotherAdd:(Int,Int) -> Void = add anotherAdd(3,4)
import Foundation func add() { } let anotherAdd:() -> Void = add anotherAdd()
import Foundation var arr = [Int]() for _ in 1...20{ arr.append(Int(arc4random()%100)) } println(arr) sorted(arr) println(sorted(arr))
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主页:https://github.com/chenyufeng1991 。欢迎大家访问!