好久沒來了,趁著新語言Swift發布,繼續鉆研中!
Create Class 創建類 (重載效果)
// Create Class 創建類class MyClass { // PRoperties 成員變量 init() { // Constructor 構造函數 } // Method 成員方法 func doIt() { println("doIt") } func doIt() -> Int { return 0 } func doIt(a:Int) -> Int { return a } func doIt(a:Int, b:Int) -> Int { return a + b } func doIt() -> String { return "" } func doIt(a:String) -> String { return a } func doIt(a:String, b:String) -> String { return a + b }}// Create / Using an Instance 創建 / 使用 一個實例var a = MyClass()a.doIt("Wang ", b: "Zhipeng")
Enums 枚舉
// Enums 枚舉enum ComcSoftType: Int { case DevelopmentEngineer = 1 case TestEngineer = 2 }var myType = ComcSoftType.DevelopmentEngineer
Declaring Variables 變量的聲明 (可選變量)
// Declaring Variables 變量的聲明var mutableDouble:Double = 1.0mutableDouble = 2.0let constantDouble:Double = 1.0//constantDouble = 2.0 Error 錯誤var autoDouble = 1.0// Optional Value 可選變量 (新機制)var optionDouble:Double? //此刻 optionDouble 根本沒有分配內存,對其取地址: &optionDouble 為NULLoptionDouble = 1.0 //這時候開始 optionDouble 才會開始分配內存if let defineDouble = optionDouble { println("已經分配內存")}else { println("沒有分配內存")}
Control Flow 控制流
// Control Flow 控制流var condition = trueif condition { println("正確")}else { println("錯誤")}var val = "Four"switch val { case "One": "One" case "Two", "Three": "Two, Three" default: "default"}// omits upper value, use ... to include 省略了上限值,使用 ... 包括for i in 0..3 { println("i = /(i)")}for var j = 0; j < 3; ++j { println("j = /(j)")}// Whilevar n = 2while n < 100 { n = n * 2}println(n)var m = 2do { m = m * 2} while m < 100println(m)
String Quick Examples 字符串的例子
// String Quick Examples 字符串的例子var firstName = "Zhipeng"var lastName = "Wang"var helloString = "Hello, /(lastName) /(firstName)"var tipString = "2499"var tipInt = tipString.toInt()extension Double { init (string:String) { self = Double(string.bridgeToObjectiveC().doubleValue) }}tipString = "24.99"var tipDouble = Double(string:tipString)
Array Quick Examples 數組的例子
// Array Quick Examples 數組的例子var person1 = "One"var person2 = "Two"var array:String[] = [person1, person2]array += "Three"for person in array { println("person: /(person)")}var personTwo = array[1]println("personTwo: /(personTwo)")
Dictionary Quick Examples 字典的例子 (泛型效果)
// Dictionary Quick Examples 字典的例子var dic:Dictionary<String, String> = ["One": "1", "Two": "2", "Three": "3"]dic["Three"] = "4" // Update Threedic["One"] = nil // Delete Onefor(key, value) in dic { println("key: /(key), value: /(value)")}
Function 函數
// Function 函數func getPirces() -> (Double, Double, Double) { return (1.1, 1.2, 1.3) // tuple 元組}// 取出元組var (one, two, three) = getPirces()println("One = /(one), Two = /(two), Three = /(three)")func getSum(numbers:Int...) -> Int { var sum = 0 for number in numbers { sum += number } return sum}getSum()getSum(1, 3, 5, 7, 9)
函數嵌套
// 函數嵌套func nestedFunction() -> Int { var x = 0 // func add() { x += 1 } // add() return x}
函數傳遞
// 函數傳遞func makeIncrementer() -> (Int -> Int) { func addOne(number:Int) -> Int { return number + 1 } return addOne}var increment = makeIncrementer()increment(1)func makeIncrementer2() -> ((String, String) -> String) { func and(a:String, b:String) -> String { return a + b } return and}var increment2 = makeIncrementer2()increment2("Wang ", "zhieng")func lessThanOne(number:Int) -> Bool { return number < 1}func makeIncrementer3(list:Int[], condition:Int -> Bool) -> Bool { for number in list { if condition(number) { return true } } return false}makeIncrementer3([1,2,3,4,5], lessThanOne)
Extends 繼承 (多態效果)
// Extends 繼承 (多態效果)println("======================= Extends 繼承")class People { // Eat 吃 func eat() { println("吃") } // Sleep 睡 func sleep() { println("睡") } // Work 工作 func work() { println("工作") } // Said 說 func said() { println("我是人") }}class Chinese:People { // Said 說 override func said() { println("我是中國人") }}class Americans:People { // Said 說 override func said() { println("我是美國人") }}class Alien { // Eat 吃 func unknown() { println("外星人") }}// 不支持多繼承//class futureGenerations:Chinese, Americans { // Multiple inheritance from classes 'Chinese' and 'Americans'// // Said 說// override func said() {// println("我是中國人和美國人的后代")// }//}// 多態 必要條件:1.繼承 2.重寫 3.父類引用指向子類對象var people = People()people.said()people = Chinese()people.said()people = Americans()people.said()//people = Alien() Error 'Alien' is not convertible to 'People'//people.said()var chinese = Chinese()chinese.said()var americans = Americans()americans.said()
新聞熱點
疑難解答