原文: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)") }
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 字典的例子
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)") }
时间: 2024-10-29 22:14:16