Swift 函数初级使用

19. December 2015 Swift 0

import Foundation

func sayHello(name:String?)->String

{

   let result = “hello” + (name ?? “guest”)

   return result

}

var ss:String!

ss = “lijian”

println(sayHello(ss))

//获取最大最小值,参数和返回值都是可选类型

func maxmin (score:[Int] ) -> (max:Int,min:Int)?

{

   if score.isEmpty

   {

       return nil

   }

   var curmax = score[0]

   var curmin = score[0]

   for sco in score[1..count]

   {

       curmax = max(curmax, sco)

       curmin = min(curmin, sco)

   }

   return (curmax,curmin)

   

}

var user:[Int]? = [10,22,3,4,55,6,77,8,8,53]

user = user ?? []

if let res = maxmin(user!)

{

   println(res.max)

   println(res.min)

} 

//—————————————— 

//func say (name na:String,sex se:String)

//定义内部参数和外部参数 

func say (#name:String,#sex:String) //既是内部参数,又是外部参数   

 

{

   println(“\(name),\(sex)”)

}

say(name: “lijian”, sex: “male”) 

//——————————————默认参数 

func hi(name:String,sex:String = “male”) ->String

{

   let res = name + ” ” + sex

   return res

}

var qq = hi(“lijian”)

println(qq)

//——————————————可变参数

func add(a:Int,b:Int,other:Int …) ->Int

{

   var res = a+b

   for s in other

   {

       res += s

   }

   return res

}

var sum = add(12, 11)

println(sum)

var summ = add(33, 323, 33,4534,333,55)

println(summ)

//——————————————转换二进制

func toBi(var su:Int)->String{

   var res:String = “”

   while su != 0{

   res = String(su%2) + res

       su /= 2

   }

   return res

}

var oo = 100

println(toBi(oo))

//—————————————— 改变参数 

func ssap (inout a:Int,inout b:Int)

{

   let t = a

   a = b

   b = t

   println(“\(a),\(b)”)

}

var x = 100,y = 33

ssap(&x, &y)

//——————————————函数作为参数传递

func change(op:(Int)->Int,inout score:[Int])

{

   for ii in  0..count

   {

       score[ii] = op(score[ii])

   }

   for ss in score{

       println(ss)

   }

}

func op1(x:Int)->Int{

   return Int(sqrt(Double(x)*10))

   

}

func op2(x:Int)->Int{

   return Int(Double(x)/150.0*100.0)

}

var score1 = [22,33,566,777,88]

change(op1,&score1) 

//——————————————自定义排序规则

//函数和闭包是引用类型

var arr = [Int]()

for _ in 0..<<span style=”font-variant-ligatures: no-common-ligatures; color: #272ad8″>20

{

   arr.append(Int(arc4random()%100))

}

sort(&arr)

sort(&arr, {a,b in return a>b})  //闭包智能写法,自动判断返回类型和参数类型

sort(&arr, {$0 > $1})

sort(&arr, >)

sort(&arr, { (a, b) -> Bool in a>b }) //以上这几种写法都可以穿点排序规则,结构一样

for yy in arr

{

   println(yy)

} 

var Str1 = [“dyf”,”dfg”,”d”,”3″,”dfghhdgdfgreg”]

sort(&Str1)

for uu in Str1{

   println(uu)

}

func length(s1:String,s2:String) -> Bool

{

   if count(s1) != count(s2)

   {

   return count(s1) > count(s2)

   }

   else{       

 

       return s1>s2

   }   

 

}

sort(&Str1, length)

for uu in Str1{

   println(uu)

}

// trailing closure

sort(&Str1){

   (s1,s2) in if count(s1) != count(s2)

   {

       return count(s1) > count(s2)

   }

   else{

       

       return s1>s2

   }

}

//—————————————–函数作为返回值

var wei = 100

func who (U:Int)->Int{

   return U*100

}

func woh (u:Int)->Int{

   return u*100

}

func price(U:Int)->(Int)->Int{

   return U<=100 ? who:woh   

 

}

var  sssss:(Int)->Int = price(wei)

println(sssss(wei))


Leave a Reply

Your email address will not be published. Required fields are marked *