学习笔记

let testScore = 86 // 常量 var grade:Character // 变量+类型

switch testScore/10{ // switch执行运算、多种情况合并 case 9: grade = "优" // 不需要break case 8: grade = "良" case 7,6: grade = "中" default: grade = "差" } println("Grade=\(grade)") // 字符串中直接格式化

switch testScore{ case 90...100: // 区间 grade = "优" case 80..<90: grade = "良" fallthrough case 60..<80: grade = "中" fallthrough // 加上这个之后就变成继续执行default语句 default: grade = "差" } println("Grade=\(grade)")

let value = 1.000 var desc:String switch value { case 0.0: // 浮点 desc = "最小值" case 0.5: desc = "中值" case 1.0: desc = "最大值" default: desc = "其他值" } println("说明=\(desc)")

let level = "优" switch level{ case "优": // 字符串 desc = "90分以上" case "良": desc = "80分-90分" case "中": desc = "70分-80分" case "差": desc = "低于60分" default: desc = "无法判断" } println("说明=\(desc)")

// 原祖 var student = (id:"1002",name:"李四",age:32,ChineseScore:90,EnglishScore:91) switch student { case (_,_,let age,90...100,90...100) where age > 0: // let age:值绑定 // where:类似于SQL的where语句 if (age > 30){ desc = "老优" } else { desc = "小优" } case (_,_,_,80..<90,80..<90): desc = "良" case (_,_,_,60..<80,60..<80): desc = "中" case (_,_,_,60..<80,90...100),(_,_,_,90...100,60..<80): desc = "偏科" case (_,_,_,0..<80,90...100),(_,_,_,90...100,0..<80): desc = "严重偏科" default: desc = "无" } println("说明=\(desc)")

// width、height:局部参数名,只能在函数内部使用。这点跟C、C++、Objective-C都不一样 // 可以指定默认参数值,而且还可以不一定是最后一个;但是【参数名】是必须得 // w、H:外部参数名,共调用的时候使用 func rectangleArea(w width:Double=30,H height:Double)->Double { let area = width*height return area } // #:代表可以把局部参数名作为外部参数名使用 func rectangleArea_same(#width:Double,#height:Double)->Double { let area = width*height return area }

println("320x480的长方形的面积:\(rectangleArea(H:480))") println("320x480的长方形的面积:\(rectangleArea_same(width:320,height:480))")

func sum(numbers:Double...)->Double{ var total:Double = 0 for number in numbers { total += number } return total } println("sum=\(sum(100.0,20,30))") println("sum=\(sum(30,20))")

// inout:参数的传递引用 func increment(inout value:Double,amount:Double=1.0){ value+=amount } var value1:Double=10.0 increment(&value1) println(value1)

increment(&value1, amount: 100.0) println(value1)

func position(dt:Double,speed:(x:Int,y:Int)) -> (x:Int,y:Int){ var posx:Int = speed.x*Int(dt) var posy:Int = speed.y*Int(dt) return (posx,posy) }

let move = position(60.0, (10,-5)) println("物体位移:\(move.x),\(move.y)")

你可能感兴趣的:(学习笔记)