import Foundation
// 閉包:閉包的本質(zhì)-匿名函數(shù)(沒有名字的函數(shù))就是一個能完成特定功能的代碼塊
//寫一個求和函數(shù)
func sum(num1:Int,num2:Int)->Int{return num1 + num2}
/* (Int, Int)->(Int) */let biBao:( (Int, Int)->(Int) )biBao = {? ? (num1:Int, num2:Int)->(Int) in? ? return num1 + num2}
//調(diào)用閉包let result = biBao(1,2)print(result)
//無參數(shù)無返回值的閉包
//OC return作用: 1:返回一個值 2:提前結(jié)束一個函數(shù)
//()->(Void) 無參無返的閉包類型
let b1:(()->(Void)) = {? ? ()->(Void) in? ? ? ?
?print("無參無返的閉包類型")}b1()//無參數(shù)有返回值的閉包類型
//() -> (Int)let b2:(() -> (Int)) = {? () -> (Int) in? ? return 10}print( b2() )
//有參數(shù)有返回值閉包類型
//() -> (Int)var b3:( (Int,Int)->(Int) ) = {? ? (n:Int,m:Int)->(Int) in? ??
return n + m}print(b3(1,5))b3 = {? ? (a:Int,b:Int)->(Int) in? ? return a - b}print( b3(10,5) )b3 = {? ? (i:Int,j:Int)->(Int) in? ? return i * j}
print(b3(2,3))
//閉包類型的變量闺兢,不止能存儲一個代碼塊猫妙,而是能存儲和變量類型一致的所有代碼塊
//有參數(shù)無返回值閉包類型//比較兩個數(shù)最大值并把兩個值輸出的閉包
let b4:((Int,Int) -> (Void)) = {? ? (x:Int,y:Int) -> (Void) in? ? print(x > y ? x : y)
? //三步運(yùn)算符}b4(10,13)var arr = ["zhangsan","lisi","yifeng","mazi","xiaofan"]
//可變數(shù)組 兩種排序方式都可以使用,不可變的只能用sorted
//arr.sort()
//arr.sorted()
//升序排//arr.sort()
//print(arr)arr.sort { (str1:String, str2:String) -> Bool in?
?// 比較條件大于勉痴,得出的結(jié)果是降序?
?// 比較條件是小于睬愤,得出的結(jié)果是升序? ?
?return str1 > str2}print(arr)//泛型? (了解)func printContent(something:T){?
?//T的位置隨便定義
print(something)
}
printContent(something: "12345")
printContent(something: 1)
printContent(something: 1.22)