博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Swift的笔记和参考
阅读量:5127 次
发布时间:2019-06-13

本文共 5033 字,大约阅读时间需要 16 分钟。

好久没来了,趁着新语言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
= ["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()

 

转载于:https://www.cnblogs.com/thefeelingofsimple/p/3768725.html

你可能感兴趣的文章
MySQL常用命令总结2
查看>>
js日期时间函数(经典+完善+实用)
查看>>
步进指令与顺控程序设计
查看>>
记一次数据库异常恢复
查看>>
随机梯度下降(Stochastic gradient descent)和 批量梯度下降(Batch gradient descent )的公式对比...
查看>>
python3(1)
查看>>
简单聊聊智能硬件的固件测试
查看>>
pat1042. Shuffling Machine (20)
查看>>
霓虹灯的效果
查看>>
学习进度六
查看>>
Spring Boot干货系列:(七)默认日志logback配置解析
查看>>
PHP - 判断php是否对表单数据内的特殊字符自动转义
查看>>
简易商城 [ html + css ] 练习
查看>>
Linux 下Makefile教程
查看>>
[转]MSP430另一种UART实现
查看>>
myeclipse部署多个web工程
查看>>
tcp_协议基础
查看>>
layui弹窗 之 iframe关闭
查看>>
【BZOJ2565】最长双回文串 Manacher
查看>>
There is no PasswordEncoder mapped for the id "null"
查看>>