import Foundation var a = 1 //變量 var b = 2 let c = a + b //常量 var str = "Hello " var s:String = "world" //指明一個數據類型 var i:Int = 10 var strs = str + "hi sam" //字符串鏈接 strs = str + "\(a)" var arr = ["hello","world",1.2322,100] //數組 //println(arr[3]) //println(arr) var arr1 = [] //建立一個空數組 var arr2 = String[]() //建立一個Stringl類型的數組 var dict = ["name":"sam","hello":"world"] //字典 dict["age"] = "\(99)" dict["sex"] = "female" //println(dict["name"]) //println(dict) //-------------------------------------for循環--------------------------------------------------------- for i in 0..100{ //循環 // println("\(i)") } var arrItem = String[]() for i in 0..100{ arrItem.append("sam \(i)"); } //println(arrItem) for value in arrItem{ //遍歷數組 // println(value) } //--------------------------------------while循環------------------------------------------------------- var j = 0 while j < 10{ // println(j) j++ } for (key,value) in dict{ //遍歷字典 // println("\(key),\(value)") } //--------------------------------------條件判斷------------------------------------------------------- for i in 0..100{ if i%2 == 0{ // println("\(i)") } } var myName:String? = " sam" //myName = nil if let name = myName{ // println("welcome \(name)") } //--------------------------------------函數------------------------------------------------------- func sayHi(name:String){ // println("hi \(name)") } //sayHi("sam") func sayHi2(name:String)->String{ return "hi \(name)" } //println(sayHi2("sam2")) var funSay = sayHi2 //println(funSay("funSam")) //返回多個參數 func getGasPrices() -> (Double, Double, Double) { return (2.3,2.4,22.11) } let (d,e,f) = getGasPrices() //println(d) //println(getGasPrices()) //--------------------------------------類------------------------------------------------------- class Hi{ func sayHi(name:String){ println("Hi \(name)") } } var hi = Hi() //hi.sayHi("sam") class Hello:Hi{ var _name:String init(name:String){ self._name = name println("構造方法") } func sayHi(){ println("hello \(self._name)") } //重寫父類函數 override func sayHi(name:String){ println("override hello \(name)") } } var hello = Hello(name: "sam") hello.sayHi() hello.sayHi("sam")