泛型
泛型代码可根据自定义需求,写出适用于任何类型、灵活且可重用的函数和类型,避免重复的代码,用一种清晰和抽象的思维表达代码的意思
1.泛型用途
- //普通的函数,用来交换两个值
- funcswapTwoInts(inouta:Int,inoutb:Int){
- lettemporaryA=a
- a=b
- b=temporaryA
- }
- varsomeInt=3
- varanotherInt=107
- swapTwoInts(&someInt,&anotherInt)
- println("someIntisnow\(someInt),andanotherIntisnow\(anotherInt)")
- //prints"someIntisnow107,andanotherIntisnow3"
- funcswapTwoStrings(inouta:String,0); background-color:inherit">b:String){
- funcswapTwoDoubles(inouta:Double,0); background-color:inherit">b:Double){
- }