Swift的笔记和参考
好久没来了,趁着新语言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.0 mutableDouble = 2.0 let constantDouble:Double = 1.0 //constantDouble = 2.0 Error 错误 var autoDouble = 1.0 // Optional Value 可选变量 (新机制) var optionDouble:Double? //此刻 optionDouble 根本没有分配内存,对其取地址: &optionDouble 为NULL optionDouble = 1.0 //这时候开始 optionDouble 才会开始分配内存 if let defineDouble = optionDouble { println("已经分配内存") } else { println("没有分配内存") }
Control Flow 控制流
// Control Flow 控制流 var condition = true if 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)") } // While var n = 2 while n < 100 { n = n * 2 } println(n) var m = 2 do { m = m * 2 } while m < 100 println(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 Three dic["One"] = nil // Delete One for(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()