├── .DS_Store ├── 1.Swift基础.playground ├── Pages │ ├── Hello World.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ ├── Swift.xcplaygroundpage │ │ └── Contents.swift │ ├── 代码注释.xcplaygroundpage │ │ └── Contents.swift │ ├── 作业.xcplaygroundpage │ │ └── Contents.swift │ ├── 元组.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ ├── 创新之处.xcplaygroundpage │ │ └── Contents.swift │ ├── 历史.xcplaygroundpage │ │ └── Contents.swift │ ├── 变量.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ ├── 可选类型.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ ├── 如何正确使用常量与变量?.xcplaygroundpage │ │ └── Contents.swift │ ├── 布尔型.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ ├── 常量赋值.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ ├── 整数型和类型推断.xcplaygroundpage │ │ └── Contents.swift │ ├── 浮点型.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ ├── 类型安全.xcplaygroundpage │ │ └── Contents.swift │ └── 练习环境及支持.xcplaygroundpage │ │ └── Contents.swift ├── contents.xcplayground └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ ├── xiaobo.xcuserdatad │ ├── UserInterfaceState.xcuserstate │ └── WorkspaceSettings.xcsettings │ └── yons.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── 10.方法详解(选修).playground ├── Pages │ ├── 实例方法.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ └── 类型方法(选修).xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline ├── contents.xcplayground └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ ├── xiaobo.xcuserdatad │ ├── UserInterfaceState.xcuserstate │ └── WorkspaceSettings.xcsettings │ └── yons.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── 11.下标(选修).playground ├── Pages │ └── 定义和使用.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline ├── contents.xcplayground └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ ├── xiaobo.xcuserdatad │ ├── UserInterfaceState.xcuserstate │ └── WorkspaceSettings.xcsettings │ └── yons.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── 12.错误处理.playground ├── Pages │ └── 定义和使用.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline ├── contents.xcplayground └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ ├── xiaobo.xcuserdatad │ ├── UserInterfaceState.xcuserstate │ └── WorkspaceSettings.xcsettings │ └── yons.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── 13.协议.playground ├── Pages │ ├── 协议作为代理(选修).xcplaygroundpage │ │ └── Contents.swift │ ├── 协议作为类型使用.xcplaygroundpage │ │ └── Contents.swift │ ├── 协议检查和转换(选修).xcplaygroundpage │ │ └── Contents.swift │ ├── 协议组合(选修).xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ ├── 多重继承.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ ├── 定义.xcplaygroundpage │ │ └── Contents.swift │ ├── 属性协议.xcplaygroundpage │ │ └── Contents.swift │ ├── 扩展和约束(选修).xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ └── 方法协议.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline ├── contents.xcplayground └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ ├── xiaobo.xcuserdatad │ └── UserInterfaceState.xcuserstate │ └── yons.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── 14.扩展.playground ├── Pages │ ├── 定义.xcplaygroundpage │ │ └── Contents.swift │ ├── 扩展下标(选修).xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ ├── 扩展协议.xcplaygroundpage │ │ └── Contents.swift │ ├── 扩展嵌套类型(选修).xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ ├── 扩展方法.xcplaygroundpage │ │ └── Contents.swift │ ├── 扩展构造器(选修).xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ └── 扩展计算属性.xcplaygroundpage │ │ └── Contents.swift ├── contents.xcplayground └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ ├── xiaobo.xcuserdatad │ └── UserInterfaceState.xcuserstate │ └── yons.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── 15.泛型.playground ├── Pages │ ├── 使用泛型前.xcplaygroundpage │ │ └── Contents.swift │ ├── 关联类型的约束(选修).xcplaygroundpage │ │ └── Contents.swift │ ├── 关联类型(选修).xcplaygroundpage │ │ └── Contents.swift │ ├── 定义.xcplaygroundpage │ │ └── Contents.swift │ ├── 泛型函数.xcplaygroundpage │ │ └── Contents.swift │ ├── 泛型参数.xcplaygroundpage │ │ └── Contents.swift │ ├── 泛型类型.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ └── 泛型约束(选修).xcplaygroundpage │ │ └── Contents.swift ├── Resources │ └── pushpop.png ├── contents.xcplayground └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ ├── xiaobo.xcuserdatad │ └── UserInterfaceState.xcuserstate │ └── yons.xcuserdatad │ ├── UserInterfaceState.xcuserstate │ ├── UserInterfaceState_20160414124711.xcuserstate │ └── UserInterfaceState_20160414124718.xcuserstate ├── 2.基础操作符.playground ├── Pages │ ├── 操作符简介.xcplaygroundpage │ │ └── Contents.swift │ ├── 数学操作符.xcplaygroundpage │ │ ├── Contents.swift │ │ ├── Resources │ │ │ └── remainderInteger_2x.png │ │ ├── Sources │ │ │ └── showImage.swift │ │ └── timeline.xctimeline │ ├── 术语.xcplaygroundpage │ │ └── Contents.swift │ ├── 比较操作符.xcplaygroundpage │ │ └── Contents.swift │ ├── 组合赋值操作符.xcplaygroundpage │ │ └── Contents.swift │ ├── 赋值操作符.xcplaygroundpage │ │ └── Contents.swift │ └── 逻辑操作符.xcplaygroundpage │ │ └── Contents.swift ├── contents.xcplayground └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ ├── xiaobo.xcuserdatad │ ├── UserInterfaceState.xcuserstate │ └── WorkspaceSettings.xcsettings │ └── yons.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── 3.字符串和字符.playground ├── Pages │ ├── 多行字符串.xcplaygroundpage │ │ └── Contents.swift │ ├── 字符.xcplaygroundpage │ │ └── Contents.swift │ ├── 字符串字面量.xcplaygroundpage │ │ └── Contents.swift │ ├── 字符串插值.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ ├── 字符串索引(选修).xcplaygroundpage │ │ └── Contents.swift │ ├── 插入和删除(选修).xcplaygroundpage │ │ └── Contents.swift │ ├── 比较字符串(选修).xcplaygroundpage │ │ └── Contents.swift │ ├── 特殊字符.xcplaygroundpage │ │ └── Contents.swift │ ├── 简介.xcplaygroundpage │ │ └── Contents.swift │ └── 连接字符串和字符.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline ├── contents.xcplayground └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ ├── xiaobo.xcuserdatad │ ├── UserInterfaceState.xcuserstate │ └── WorkspaceSettings.xcsettings │ └── yons.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── 4.集合类型.playground ├── Pages │ ├── Set.xcplaygroundpage │ │ ├── Contents.swift │ │ ├── Resources │ │ │ ├── setEulerDiagram_2x.png │ │ │ └── setVennDiagram_2x.png │ │ ├── Sources │ │ │ └── showSet.swift │ │ └── timeline.xctimeline │ ├── 字典.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ ├── 数组.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ └── 简介.xcplaygroundpage │ │ ├── Contents.swift │ │ ├── Resources │ │ └── CollectionTypes_intro_2x.png │ │ ├── Sources │ │ └── showImage.swift │ │ └── timeline.xctimeline ├── contents.xcplayground └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ ├── xiaobo.xcuserdatad │ ├── UserInterfaceState.xcuserstate │ └── WorkspaceSettings.xcsettings │ └── yons.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── 5.控制流.playground ├── Pages │ ├── for循环.xcplaygroundpage │ │ └── Contents.swift │ ├── if和循环控制.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ ├── switch.xcplaygroundpage │ │ ├── Contents.swift │ │ ├── Sources │ │ │ └── New File.swift │ │ └── timeline.xctimeline │ ├── while循环(选修).xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ ├── 早退语句(选修).xcplaygroundpage │ │ └── Contents.swift │ └── 简介.xcplaygroundpage │ │ └── Contents.swift ├── Resources │ ├── coordinate-2.png │ └── coordinate.png ├── contents.xcplayground └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ ├── xiaobo.xcuserdatad │ ├── UserInterfaceState.xcuserstate │ └── WorkspaceSettings.xcsettings │ └── yons.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── 6.函数、闭包和高阶函数.playground ├── Pages │ ├── 参数和返回值.xcplaygroundpage │ │ └── Contents.swift │ ├── 定义函数.xcplaygroundpage │ │ └── Contents.swift │ ├── 简介.xcplaygroundpage │ │ └── Contents.swift │ └── 高阶函数.xcplaygroundpage │ │ └── Contents.swift ├── contents.xcplayground └── playground.xcworkspace │ ├── contents.xcworkspacedata │ ├── xcshareddata │ └── WorkspaceSettings.xcsettings │ └── xcuserdata │ ├── xiaobo.xcuserdatad │ ├── UserInterfaceState.xcuserstate │ └── WorkspaceSettings.xcsettings │ └── yons.xcuserdatad │ ├── UserInterfaceState.xcuserstate │ └── WorkspaceSettings.xcsettings ├── 7.枚举.playground ├── Pages │ ├── 定义和使用.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ └── 简介.xcplaygroundpage │ │ └── Contents.swift ├── contents.xcplayground └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ ├── xiaobo.xcuserdatad │ ├── UserInterfaceState.xcuserstate │ └── WorkspaceSettings.xcsettings │ └── yons.xcuserdatad │ ├── UserInterfaceState.xcuserstate │ └── UserInterfaceState_20160414134457.xcuserstate ├── 8.面向对象.playground ├── Pages │ ├── 初始化、构造器.xcplaygroundpage │ │ └── Contents.swift │ ├── 概念.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ └── 继承、重载和计算属性.xcplaygroundpage │ │ └── Contents.swift ├── contents.xcplayground └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ ├── xiaobo.xcuserdatad │ ├── UserInterfaceState.xcuserstate │ └── WorkspaceSettings.xcsettings │ └── yons.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── 9.属性详解(选修).playground ├── Pages │ ├── 实例属性.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ └── 类型属性(选修).xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline ├── contents.xcplayground └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ ├── xiaobo.xcuserdatad │ ├── UserInterfaceState.xcuserstate │ └── WorkspaceSettings.xcsettings │ └── yons.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── JSON.playground.zip ├── README.md ├── 大作业参考:房贷计算器.playground ├── Contents.swift ├── contents.xcplayground ├── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ └── yons.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── timeline.xctimeline └── 第1天大作业.playground ├── Contents.swift ├── contents.xcplayground └── playground.xcworkspace ├── contents.xcworkspacedata └── xcuserdata └── yons.xcuserdatad └── UserInterfaceState.xcuserstate /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/.DS_Store -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/Hello World.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Hello World 2 | 3 | 4 | print(1) 5 | print("hello world") 6 | //: [>](@next) 7 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/Hello World.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/Swift.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Swift - 雨燕,敏捷,快速 2 | //: - 新一代苹果平台(iOS/macOS/watchOS/tvOS)的App及游戏开发语言。 3 | //: - 语法与JavaScript、C#等语法类似,上手极快。 4 | 5 | 6 | //: [历史>](@next) 7 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/代码注释.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 代码注释 2 | //: - 有助于自己理解 3 | //: - 便于他人理解 4 | //: - 专业优雅 5 | 6 | // 定义一个圆周率常量,精度为小数点后2位 7 | let π = 3.14 8 | 9 | // 定义了一个上海地区的白天电费 10 | let 电费 = 0.5 11 | 12 | 13 | 14 | //: [>](@next) 15 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/作业.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | 2 | 3 | //随堂作业:尝试使用Int,Double,Bool,元组来表达你生活中碰到的变量或常量,至少10项。(15分钟) 4 | let student = (成绩:113,评分:9.3,好学生:false) 5 | 6 | 7 | 8 | //: [>](@next) 9 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/元组.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 元组(Tuple): 定义变量的一个组合. 2 | 3 | //: - 形式(一般): (3, "天","Swift","4") 4 | //: - 形式(前缀): (day:3, unit:"天", lang:"Swift", ver:"4") 5 | let x = 3 6 | let y = 4 7 | let z = 5 8 | 9 | let course = (day: 3, unit: "day", lang : "swift", ver: 4) 10 | course.day 11 | course.lang 12 | 13 | 14 | 15 | 16 | var 课程名 = (day:3, unit:"天", lang:"Swift", ver:"4") 17 | 课程名.day 18 | 课程名.unit 19 | 课程名.ver 20 | 21 | //: - 要获取其中的某个值,用序号或前缀 22 | 23 | 24 | 25 | 26 | //: [>](@next) 27 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/元组.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/创新之处.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Swift的创新 2 | //: - 引入 Optional(可选类型)。 3 | //: - 支持传统的“面向对象编程”,提升至“面向协议编程”;即由“类”到“值”,提倡使用结构体(struct)取代类(class),"遵守协议"取代"继承"。 4 | 5 | 6 | 7 | //: [>](@next) 8 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/历史.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 历史 2 | //: - 2014年6月推出第一个版本,由苹果员工Chris Lattern历经4年发明。 3 | //: - Swift已开源, 计划逐渐替代Objective C语言,成为iOS开发的主流语言。 4 | //: - 长期致力于成为未来20年内运行到每一部智能设备上。 5 | 6 | 7 | //: [>](@next) 8 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/变量.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 变量 2 | //: - 可主动变更或者预期范围内不固定,比如加班时间、股票指数、驾照通过几率。定义方法:var 变量名 = 值 3 | 4 | 5 | //随堂作业:把你身边的事物,用常量和变量表达出来,至少10项。(15分钟)。 6 | 7 | //: [>](@next) 8 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/变量.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/可选类型.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 可选类型(Optional): 代表变量可能有值的情况。 2 | //: - 如:用户资料的选填部分,如住址/性别等辅助信息 3 | //: - 形式: var 变量 : 类型? ,默认是无值(nil) 4 | var boboAddress: String? 5 | 6 | boboAddress = "上海市嘉定区众仁路399号A座701" 7 | 8 | 9 | 10 | 11 | 12 | //: [>](@next) 13 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/可选类型.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/如何正确使用常量与变量?.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 如何正确使用常量与变量? 2 | //: - 优先使用常量。 3 | //: - 编译器决定。 4 | 5 | var 课程名 = "3天学会Swift 3.1" 6 | 课程名 = "3天学会Swift 4" 7 | 8 | 9 | 10 | 11 | //: [>](@next) 12 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/布尔型.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 布尔型: Bool, 表示逻辑上的"真"或"假" 2 | //: - 只有2个值,true 和 false 3 | 4 | var win = true 5 | win = false 6 | 7 | let 有女友 = false 8 | 9 | let isHidden = true 10 | 11 | let insufficent = true 12 | 13 | 14 | var vip = false 15 | vip = true 16 | 17 | //: [>](@next) 18 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/布尔型.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/常量赋值.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 常量:预期范围内不变的量。 2 | //: - 给常量一个名字并赋予值,也就是定义了一个常量。 3 | //: - 形式: let 常量名 = 值 4 | 5 | //自定义常量 6 | let 女友数量 = 0 7 | 8 | let earthToSun = 1 9 | 10 | //预定义:8位整数型的区间 11 | Int.min...Int.max 12 | 13 | //公认:圆周率 14 | Double.pi 15 | 16 | //函数的结果 17 | import Foundation 18 | cos(0.0) 19 | 20 | 21 | 22 | //: [>](@next) 23 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/常量赋值.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 11 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/整数型和类型推断.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 值的类型和类型推断 2 | //: - 3这样的整数,在Swift中称为Int类型的值 3 | //: - 形式: var 变量名 : Int = 3 .由于Swift有类型推断, 类型可以省略不写. 4 | var 跑路多少公里 = 5 5 | 跑路多少公里 = 6 6 | 7 | //: [>](@next) 8 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/浮点型.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 浮点型 (小数) 2 | //: - Swift中默认浮点型是Double(双精度) 3 | 4 | var price = 2.5 5 | 6 | 7 | //: [>](@next) 8 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/浮点型.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/类型安全.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 类型安全 2 | //: - 变量一旦定义,其类型不可更改, 即: 不能给变量一个类型不同的值. 3 | 4 | 5 | 6 | 7 | var 电费 = 0.5 8 | //电费 = "五毛" 9 | 10 | print(电费) 11 | 12 | 13 | //: [>](@next) 14 | -------------------------------------------------------------------------------- /1.Swift基础.playground/Pages/练习环境及支持.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 3天学会Swift 4 2 | //: 练习环境: 3 | //: - 至少XCode 9或[Swift Sandbox](http://swiftlang.ng.bluemix.net/) 4 | //: - [Swift 4 快照下载](https://swift.org/download/) 5 | //: ---- 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | //: [>](@next) 14 | -------------------------------------------------------------------------------- /1.Swift基础.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /1.Swift基础.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /1.Swift基础.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/1.Swift基础.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /1.Swift基础.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | BuildLocationStyle 6 | UseAppPreferences 7 | CustomBuildLocationType 8 | RelativeToDerivedData 9 | DerivedDataLocationStyle 10 | Default 11 | IssueFilterStyle 12 | ShowActiveSchemeOnly 13 | LiveSourceIssuesEnabled 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /1.Swift基础.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/1.Swift基础.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /10.方法详解(选修).playground/Pages/实例方法.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ---- 2 | //: - [小波说雨燕 3天学会Swift 4](http://www.xiaoboswift.com) 3 | //: - 全集课件:[github.com/yagamis](http://github.com/yagamis/swift2basic) 4 | //: ---- 5 | //: - 方法:对象的行为,定义中是函数. 6 | //: - 实例方法: 7 | //: ---- 8 | //: - 定义和使用:与函数相似. 💡self属性:引用实例自身,通常可以不写. 9 | import UIKit 10 | 11 | //情绪 12 | enum Emotion { 13 | case happy,normal,angry,sad 14 | } 15 | 16 | //根据情绪来着装 17 | struct Dressing { 18 | var emotion : Emotion 19 | 20 | func color() -> UIColor { 21 | switch self.emotion { 22 | case .happy: 23 | return UIColor.yellow 24 | case .angry: 25 | return UIColor.red 26 | case .sad: 27 | return UIColor.blue 28 | default: 29 | return UIColor.white 30 | } 31 | } 32 | } 33 | 34 | 35 | let todayDress = Dressing(emotion: .happy) 36 | todayDress.color() 37 | 38 | //: [>](@next) 39 | -------------------------------------------------------------------------------- /10.方法详解(选修).playground/Pages/实例方法.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /10.方法详解(选修).playground/Pages/类型方法(选修).xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 类型方法 : 属于类型本身通用的方法, 与具体的实例无关。 在方法前加static 2 | //: - ⚠️对于类, 加关键字class. 可被子类重写(override) 3 | class China { 4 | static var countryName = "中国" 5 | 6 | class func greetings() { 7 | print("\(countryName)欢迎您!") 8 | } 9 | } 10 | 11 | class Anhui : China { 12 | static let proviceName = "安徽" 13 | 14 | override class func greetings() { 15 | print("\(countryName)\(proviceName),欢迎您!") 16 | } 17 | } 18 | 19 | China.greetings() 20 | Anhui.greetings() 21 | 22 | -------------------------------------------------------------------------------- /10.方法详解(选修).playground/Pages/类型方法(选修).xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /10.方法详解(选修).playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /10.方法详解(选修).playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /10.方法详解(选修).playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/10.方法详解(选修).playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /10.方法详解(选修).playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | BuildLocationStyle 6 | UseAppPreferences 7 | CustomBuildLocationType 8 | RelativeToDerivedData 9 | DerivedDataLocationStyle 10 | Default 11 | IssueFilterStyle 12 | ShowActiveSchemeOnly 13 | LiveSourceIssuesEnabled 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /10.方法详解(选修).playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/10.方法详解(选修).playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /11.下标(选修).playground/Pages/定义和使用.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ---- 2 | //: - [小波说雨燕 3天学会Swift 4](http://www.xiaoboswift.com) 3 | //: - 全集课件:[github.com/yagamis](http://github.com/yagamis/swift2basic) 4 | //: ---- 5 | //: - 下标是一种特殊的方法,通常用于访问集合或序列类型中的元素。 6 | //: - 用法: 实例名[索引] 。可以访问或设置其中元素。 7 | //: ---- 8 | //: - 定义:一个名为subscript的计算属性; 9 | // subscript(index: 参数类型) -> 返回类型 { 10 | // get {} 11 | // set {} 12 | // } 13 | //: - ☀️常见的用法:字典、数组、集合等。 14 | 15 | var odds = [1,3,5,7,9] 16 | odds[3] 17 | 18 | let citys = ["春":"昆明", "蓉": "成都", "羊":"广州"] 19 | citys["沪"] 20 | 21 | //: - 1⃣️通过下标简化调用方法调用 22 | 23 | import Foundation 24 | 25 | struct Sphere { 26 | //体积 27 | subscript(r: Double) -> Double { 28 | return (4/3) * .pi * pow(r, 3) 29 | } 30 | } 31 | 32 | let earth = Sphere() 33 | earth[6371] 34 | 35 | 36 | //: - 2⃣️多维下标。实现一个二维行列式矩阵(选修) 37 | // 第0列 第1列 38 | // 第0行 1 3 索引:当前列 + 0 39 | // 第1行 5 7 索引:当前列 + 之前所有行列总数(当前行 × 总列数) 40 | // 41 | // 通过变换成Int数组[1, 3, 5, 7],以索引来访问.⚠️确保数组索引不得越界。 42 | struct Matrix { 43 | var rows, cols : Int 44 | var grid: [Int] 45 | 46 | init(rows: Int, cols: Int) { 47 | self.cols = cols 48 | self.rows = rows 49 | 50 | grid = Array(repeating: 0, count: rows * cols) 51 | } 52 | 53 | func isValidIndex(row:Int,col:Int) -> Bool { 54 | return row >= 0 && row < rows && col >= 0 && col < cols 55 | } 56 | 57 | subscript(row:Int, col:Int) ->Int { 58 | get { 59 | assert(isValidIndex(row: row, col: col), "数组索引越界") 60 | return grid[col + (row * cols)] 61 | } 62 | set { 63 | assert(isValidIndex(row: row, col: col), "数组索引越界") 64 | 65 | grid[col + (row * cols)] = newValue 66 | } 67 | } 68 | } 69 | 70 | var matrix1 = Matrix(rows: 3, cols: 3) 71 | matrix1[0,0] = 1 72 | matrix1[0,1] = 3 73 | matrix1[0,2] = 5 74 | 75 | matrix1[1,0] = 2 76 | matrix1[1,1] = 4 77 | matrix1[1,2] = 6 78 | 79 | matrix1[2,0] = -1 80 | matrix1[2,1] = -3 81 | matrix1[2,2] = -5 82 | 83 | matrix1.grid 84 | 85 | matrix1[2,2] 86 | 87 | //: ---- 88 | //: 89 | 90 | 91 | 92 | //: [](@next) 93 | -------------------------------------------------------------------------------- /11.下标(选修).playground/Pages/定义和使用.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 14 | 15 | 20 | 21 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /11.下标(选修).playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /11.下标(选修).playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /11.下标(选修).playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/11.下标(选修).playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /11.下标(选修).playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | BuildLocationStyle 6 | UseAppPreferences 7 | CustomBuildLocationType 8 | RelativeToDerivedData 9 | DerivedDataLocationStyle 10 | Default 11 | IssueFilterStyle 12 | ShowActiveSchemeOnly 13 | LiveSourceIssuesEnabled 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /11.下标(选修).playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/11.下标(选修).playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /12.错误处理.playground/Pages/定义和使用.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ---- 2 | //: - [小波说雨燕 3天学会Swift 4](http://www.xiaoboswift.com) 3 | //: - 全集课件:[github.com/yagamis](http://github.com/yagamis/swift2basic) 4 | //: ---- 5 | //: 错误处理: 反映运行出错的“细节”,并恢复程序的过程. 6 | //: - 可选类型(Optional)只处理值缺失的情况, 错误处理可以针对不同的出错原因对不同的应对. 7 | 8 | //: - 一个函数可以加上throws关键字,表示可以处理错误. 这个函数的调用者可以捕获(catch)这个错误并进行应对. 9 | func learning() throws { 10 | print("3天学会Swift 4") 11 | } 12 | 13 | //: - 当你调用可以抛出错误的函数,须在前面加上try关键字. 14 | try learning() 15 | 16 | //: - 处理更细分的错误情况,错误类型须遵从Error协议。 17 | enum LearningError: Error { 18 | case noWay, dislikeReading, lack(tool: String) 19 | } 20 | 21 | func iosDev(hasWay: Bool, byReading: Bool, hasTool: Bool) throws { 22 | guard hasWay else { 23 | throw LearningError.noWay 24 | } 25 | 26 | guard byReading else { 27 | throw LearningError.dislikeReading 28 | } 29 | 30 | guard hasTool else { 31 | throw LearningError.lack(tool: "Mac book电脑") 32 | } 33 | } 34 | 35 | var funds = 7000 36 | 37 | func buy(_ tool:String) { 38 | if funds >= 6000 { 39 | funds -= 6000 40 | print("您已经采购",tool,"花费6000元,余额",funds) 41 | } else { 42 | print("资金不足!") 43 | } 44 | } 45 | 46 | do { 47 | try iosDev(hasWay: true, byReading: true, hasTool: false) 48 | print("迈出Swift学习第一步!") 49 | } catch LearningError.noWay { 50 | print("找小波") 51 | } catch LearningError.dislikeReading { 52 | print("看视频学习") 53 | } catch LearningError.lack(let mac) { 54 | buy(mac) 55 | } 56 | 57 | //: - 有时候仅关心结果有无,可使用 try? 来忽略错误细节。 58 | if let _ = try? iosDev(hasWay: true, byReading: false, hasTool: true) { 59 | print("恭喜您已经开始学习!") 60 | } else { 61 | print("条件不足,无法学习") 62 | } 63 | 64 | // try! 强制执行可能会出错的方法,如果出错,则程序崩溃。 65 | try! iosDev(hasWay: true, byReading: true, hasTool: true) 66 | 67 | -------------------------------------------------------------------------------- /12.错误处理.playground/Pages/定义和使用.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 14 | 15 | 20 | 21 | 25 | 26 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /12.错误处理.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /12.错误处理.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /12.错误处理.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/12.错误处理.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /12.错误处理.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | BuildLocationStyle 6 | UseAppPreferences 7 | CustomBuildLocationType 8 | RelativeToDerivedData 9 | DerivedDataLocationStyle 10 | Default 11 | IssueFilterStyle 12 | ShowActiveSchemeOnly 13 | LiveSourceIssuesEnabled 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /12.错误处理.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/12.错误处理.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /13.协议.playground/Pages/协议作为代理(选修).xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 协议作为代理: 代理是一种常见的设计模式, 可以让类或结构体把一部分职责,指派给非同类的实例去承担. 2 | //: - 若要寻求代理,可以通过定义一个协议,打包要实现的职责于其中. 3 | //: - 该代理协议的遵从者就可以实现这些打包的职责. 4 | //: - 代理在iOS开发中,一般可以用于响应特定的操作,或从外部数据源取回数据但无需了解是何种数据源. 5 | //游戏角色 6 | struct Role { 7 | var name: String 8 | var attack: Int 9 | var defence: Int 10 | } 11 | 12 | //玩游戏 13 | protocol Lolgame { 14 | var role: Role { get } 15 | mutating func play() 16 | } 17 | 18 | //游戏代练协议 19 | protocol LolgameDelegate { 20 | func gameStart(game: Lolgame) -> Int 21 | func gameRolePK(game: Lolgame, armed: Bool) -> Int 22 | func gameOver(game: Lolgame) -> Int 23 | } 24 | 25 | //实现游戏代练 26 | struct GameAgent: LolgameDelegate { 27 | func gameStart(game: Lolgame) -> Int { 28 | print(game.role.name,"开始打怪升级","起始经验:2000") 29 | return 2000 30 | } 31 | 32 | func gameRolePK(game: Lolgame, armed: Bool) -> Int { 33 | if armed { 34 | print(game.role.name, "您有武器,获得经验:5000") 35 | return 5000 36 | } else { 37 | print(game.role.name, "您赤手空拳,只获得经验:2500,有武器加倍哦!") 38 | return 2500 39 | } 40 | } 41 | 42 | func gameOver(game: Lolgame) -> Int { 43 | print(game.role.name, "您已经正常退出,获得经验1000") 44 | return 1000 45 | } 46 | } 47 | 48 | struct PlayGame: Lolgame { 49 | var exp: Int 50 | var gameAgent: GameAgent? 51 | 52 | let role = Role(name: "战神小波", attack: 2000, defence: 5000) 53 | 54 | mutating func play() { 55 | if let gameAgent = gameAgent { 56 | print("您使用了代练!") 57 | 58 | exp += gameAgent.gameStart(game: self) 59 | exp += gameAgent.gameRolePK(game: self, armed: true) 60 | exp += gameAgent.gameOver(game: self) 61 | } else { 62 | print("没有使用任何代练或挂机,不会自动升级") 63 | } 64 | 65 | print("一共经验值是:", exp) 66 | } 67 | } 68 | 69 | //🌰 70 | 71 | var playgame1 = PlayGame(exp: 0, gameAgent: nil) 72 | playgame1.play() 73 | 74 | let agentSmith = GameAgent() 75 | 76 | playgame1.gameAgent = agentSmith 77 | 78 | playgame1.play() 79 | 80 | //: [协议扩展和扩展约束](@next) 81 | -------------------------------------------------------------------------------- /13.协议.playground/Pages/协议作为类型使用.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 协议作为类型使用: 可用于参数类型/返回类型; 变量/常量/属性; 集合类型中的元素类型 2 | protocol Demo { 3 | func showType() 4 | } 5 | 6 | struct A: Demo { 7 | func showType() { 8 | print("struct A") 9 | } 10 | } 11 | 12 | class B: Demo { 13 | func showType() { 14 | print("class b") 15 | } 16 | } 17 | 18 | let a = A() 19 | let b = B() 20 | 21 | let arrayDemo: [Demo] = [a, b] 22 | 23 | //: [>](@next) 24 | -------------------------------------------------------------------------------- /13.协议.playground/Pages/协议检查和转换(选修).xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 协议检查和转换: 使用is和as类型转换操作符来检查协议遵从与否,或转换成特定的协议. 2 | protocol Coder { 3 | var skill : String { get } 4 | } 5 | 6 | protocol Nameable { 7 | var name: String { get } 8 | } 9 | 10 | struct DotnetCoder: Coder, Nameable { 11 | var name: String 12 | var skill: String { 13 | return "C#" 14 | } 15 | } 16 | 17 | struct PhpCoder: Coder, Nameable { 18 | var name: String 19 | var skill: String { 20 | return "PHP" 21 | } 22 | } 23 | 24 | struct Beginner: Nameable { 25 | var name: String 26 | } 27 | 28 | let bossXu = DotnetCoder(name: "陈总") 29 | let bossChen = PhpCoder(name: "徐总") 30 | let beginner = Beginner(name: "小波") 31 | 32 | let coders : [Any] = [bossXu, bossChen, beginner] 33 | 34 | for coder in coders { 35 | if let coder1 = coder as? Coder & Nameable { 36 | print(coder1.skill + "老司机" + coder1.name) 37 | } else { 38 | print("你根本不是司机!") 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /13.协议.playground/Pages/协议组合(选修).xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 协议组合: 多个协议临时组合在一起的类型. 形式: 协议1 & 协议2 & ...> 2 | protocol Ageable { 3 | var age: Int {get} 4 | } 5 | 6 | protocol Nameable { 7 | var fullName: String { get } 8 | } 9 | 10 | struct Student: Ageable, Nameable { 11 | var age: Int 12 | var fullName: String 13 | } 14 | 15 | struct Teacher: Ageable, Nameable { 16 | var age: Int 17 | var fullName: String 18 | } 19 | 20 | 21 | let wang = Teacher(age: 30, fullName: "王老师") 22 | 23 | func wish(person: Student) { 24 | print("祝",person.fullName,person.age,"岁生日快乐!") 25 | } 26 | 27 | //wish(person: wang) 28 | 29 | func wish2(person: Ageable & Nameable) { 30 | print("祝",person.fullName,person.age,"岁生日快乐!") 31 | } 32 | 33 | wish2(person: wang) 34 | 35 | let xiaoming = Student(age: 12, fullName: "晓明") 36 | wish(person: xiaoming) 37 | 38 | 39 | //: [>](@next) 40 | -------------------------------------------------------------------------------- /13.协议.playground/Pages/协议组合(选修).xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /13.协议.playground/Pages/多重继承.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 💡协议继承: 一个协议可以继承若干个协议,并可以在继承基础上增加新需求。(面向协议编程) 2 | protocol Nameable { 3 | var name : String { get } 4 | } 5 | 6 | protocol Ageable { 7 | var age : Int { get } 8 | } 9 | 10 | protocol LegalCitizen: Nameable, Ageable { 11 | var idcardNumber: Int { get } 12 | } 13 | 14 | struct Coder: LegalCitizen { 15 | var name: String 16 | var age: Int 17 | var idcardNumber: Int 18 | } 19 | 20 | 21 | 22 | //: [>](@next) 23 | -------------------------------------------------------------------------------- /13.协议.playground/Pages/多重继承.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /13.协议.playground/Pages/定义.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ---- 2 | //: - [小波说雨燕 3天学会Swift 4](http://www.xiaoboswift.com) 3 | //: - 全集课件:[github.com/yagamis](http://github.com/yagamis/swift2basic) 4 | //: ---- 5 | //: 协议:方法、属性或一段功能的蓝本。 6 | //: - 任意类型都可以“领养”协议,从而实现蓝本; 也称“遵从”该协议。 7 | //: - Swift的特色:面向协议编程, 鼓励多“领养”协议来替代类继承。 因为“领养”的代价远低于继承。 8 | 9 | //2个空协议 10 | protocol HouseAffordable {} 11 | protocol CarAffordable {} 12 | 13 | //: - “领养/遵从”若干个协议,用逗号分隔 14 | struct Marriage: HouseAffordable, CarAffordable {} 15 | 16 | //: - 如果同时有继承和遵从协议,协议写在父类之后。 17 | class Father {} 18 | 19 | class Son: Father, HouseAffordable, CarAffordable {} 20 | 21 | //: [>](@next) 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /13.协议.playground/Pages/属性协议.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 属性协议:顾名思义,遵从者要实现协议规定的属性。 2 | //: - 属性必须定义为变量。 setter可选实现。 3 | protocol Nameable { 4 | var nick: String { get } 5 | var title : String { get } 6 | } 7 | 8 | enum Grade { 9 | case primary, middle 10 | } 11 | 12 | //: - 遵从属性协议: 必须要实现其规定的属性 13 | struct Student: Nameable { 14 | var grade : Grade 15 | var nick: String 16 | 17 | var title: String { 18 | switch grade { 19 | case .primary: 20 | return "小学生" + nick 21 | case .middle: 22 | return "中学生" + nick 23 | } 24 | } 25 | } 26 | 27 | let xiaoming = Student(grade: .primary, nick: "晓明明") 28 | xiaoming.title 29 | 30 | //这个例子的好处在哪里? 把名字相关属性从Student分离 31 | struct Teacher: Nameable { 32 | var nick: String 33 | var title: String { 34 | return nick + "老师" 35 | } 36 | } 37 | 38 | let wanglaoshi = Teacher(nick: "老王") 39 | wanglaoshi.title 40 | 41 | //: [>](@next) 42 | -------------------------------------------------------------------------------- /13.协议.playground/Pages/扩展和约束(选修).xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 💡协议扩展: 即使无源码权限下,给已有的类型添加协议. 2 | protocol ToStr { 3 | var str: String {get} 4 | } 5 | 6 | extension Double: ToStr { 7 | var str: String { 8 | return "\(self)" 9 | } 10 | } 11 | //: - 既存实例会自动遵从刚添加的协议. 12 | 3.14.str 13 | 14 | //: ⚡️扩展约束: 可以给要扩展的类型加上限定条件(where)。 15 | extension ToStr where Self: CustomStringConvertible { 16 | var myDesc: String { 17 | return "值:" + self.description 18 | } 19 | } 20 | 21 | 2.3.myDesc 22 | 23 | //集合类型Collection也是一种协议, Element指代其中的元素 24 | extension Collection where Element : CustomStringConvertible { 25 | var desc : String { 26 | let itemAsText = self.map{ $0.description } 27 | return "元素数: \(self.count),包含:" + itemAsText.joined(separator: ", ") 28 | } 29 | } 30 | 31 | let numbers = [1, 2, 3] 32 | print(numbers.desc) 33 | 34 | //: [>](@next) 35 | -------------------------------------------------------------------------------- /13.协议.playground/Pages/扩展和约束(选修).xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /13.协议.playground/Pages/方法协议.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 方法协议: 定义时没有花括号执行体. 实现仅要求名称相同. 2 | protocol Randomable { 3 | func number() -> Int 4 | } 5 | 6 | import Foundation 7 | 8 | struct Dice: Randomable { 9 | func number() -> Int { 10 | return Int(arc4random()) % 6 + 1 11 | } 12 | } 13 | 14 | let dice1 = Dice() 15 | 16 | for _ in 1...30 { 17 | print(dice1.number()) 18 | } 19 | 20 | //: [>](@next) 21 | -------------------------------------------------------------------------------- /13.协议.playground/Pages/方法协议.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /13.协议.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /13.协议.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /13.协议.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/13.协议.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /13.协议.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/13.协议.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /14.扩展.playground/Pages/定义.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ---- 2 | //: - [小波说雨燕 3天学会Swift 4](http://www.xiaoboswift.com) 3 | //: - 全集课件:[github.com/yagamis](http://github.com/yagamis/swift2basic) 4 | //: ---- 5 | //: 扩展: 为既有类型添加新功能,即使无权查看源代码(也叫:追加建模) 6 | //: - 范围: 扩展计算属性,方法(包括构造器、下标); 扩展嵌套类型; 扩展协议。 7 | 8 | //: - 一种分离代码的“模式”:如果一个类其中的属性和方法过多(iOS开发中通常ViewController代码量很大),应当考虑把代码分离。写成扩展是一种很直接的方式。(见iOS 11开发第一季中的FansArea例子) 9 | 10 | 11 | 12 | 13 | //: [>](@next) 14 | -------------------------------------------------------------------------------- /14.扩展.playground/Pages/扩展下标(选修).xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 扩展下标: 给已有类型加上一个新下标. 2 | 3 | //: - 对整数加从右往左的索引下标 4 | //🌰 123456789[2] 算法,除以100,对10求余数, 就是7 5 | 6 | extension Int { 7 | subscript(index: Int) -> Int { 8 | var base = 1 9 | 10 | for _ in 1...index { 11 | base *= 10 12 | } 13 | 14 | return self / base % 10 15 | } 16 | } 17 | 123456789[2] 18 | 19 | //: [扩展嵌套类型](@next) 20 | -------------------------------------------------------------------------------- /14.扩展.playground/Pages/扩展下标(选修).xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /14.扩展.playground/Pages/扩展协议.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 让既有类型遵从协议。 2 | 3 | //天气预报 4 | struct Weather { 5 | var name: String 6 | var temperature: Double 7 | var description: String 8 | } 9 | 10 | //国内天气目前不含 露点温度 11 | protocol DewTempMeasurable {} 12 | //云量 13 | protocol CloudAmoutable {} 14 | 15 | //扩展天气预报模型 16 | extension Weather: DewTempMeasurable, CloudAmoutable { 17 | 18 | } 19 | 20 | 21 | -------------------------------------------------------------------------------- /14.扩展.playground/Pages/扩展嵌套类型(选修).xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 扩展嵌套类型: 可以对既有类型添加嵌套类型. 2 | //: - 🌰对整数添加正负类型判断 3 | extension Int { 4 | enum Kind { 5 | case 正,负,零 6 | } 7 | 8 | var 正负类型: Kind { 9 | switch self { 10 | case 0: 11 | return .零 12 | case let x where x > 0: 13 | return .正 14 | default: 15 | return .负 16 | } 17 | } 18 | } 19 | 20 | let 整数组 = [3, 4, 0 , -5] 21 | var 整数组类型 = [String]() 22 | 23 | for 整数 in 整数组 { 24 | var 符号: String 25 | 26 | switch 整数.正负类型 { 27 | case .正: 28 | 符号 = "+" 29 | case .负: 30 | 符号 = "-" 31 | default: 32 | 符号 = "0" 33 | } 34 | 35 | 整数组类型.append(符号) 36 | } 37 | 38 | 整数组类型 39 | 40 | 41 | 42 | //: [](@next) 43 | -------------------------------------------------------------------------------- /14.扩展.playground/Pages/扩展嵌套类型(选修).xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /14.扩展.playground/Pages/扩展方法.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 扩展方法: 为既有类型添加方法 2 | extension Int { 3 | func repeatDo(closure: () -> Void) { 4 | for _ in 1...self { 5 | closure() 6 | } 7 | } 8 | } 9 | 10 | 4.repeatDo { 11 | print("3天学会Swift 4 之扩展") 12 | } 13 | 14 | //: - 如果方法要结修改结构体(或枚举)实例的属性时,要加mutating关键字 15 | extension Int { 16 | mutating func square() { 17 | self *= self 18 | } 19 | } 20 | 21 | var a = 3 22 | a.square() 23 | a 24 | 25 | //作业:根据你的理解和想象,对String、Int、Double、集合类型等基本类型扩展几个方法和属性(30分钟) 26 | 27 | //: [>](@next) 28 | -------------------------------------------------------------------------------- /14.扩展.playground/Pages/扩展构造器(选修).xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 扩展构造器: 给既有类型扩展新构造器, 如自定义的初始化参数,或原本不支持的初始化选项. 2 | struct Weapon { 3 | var atk = 0, dex = 0 4 | } 5 | 6 | struct Equip { 7 | var def = 0, agl = 0 8 | } 9 | 10 | struct Role { 11 | var weapon = Weapon() 12 | var equip = Equip() 13 | } 14 | 15 | let xiaobo = Role() 16 | 17 | let xida = Role(weapon: Weapon(atk: 300, dex: 5), equip: Equip(def: 200, agl: 3)) 18 | 19 | //土豪玩家 VIP1 20 | extension Role { 21 | init(vip1weapon: Weapon, vip1Equip: Equip) { 22 | let vipatk = vip1weapon.atk * 2 23 | let vipdef = vip1Equip.def * 2 24 | 25 | self.init(weapon: Weapon(atk: vipatk, dex: vip1weapon.dex), equip: Equip(def: vipdef, agl: vip1Equip.agl)) 26 | } 27 | } 28 | 29 | let tuhao1 = Role(vip1weapon: Weapon(atk: 300, dex: 5), vip1Equip: Equip(def: 200, agl: 3)) 30 | 31 | 32 | 33 | //: [>](@next) 34 | -------------------------------------------------------------------------------- /14.扩展.playground/Pages/扩展构造器(选修).xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /14.扩展.playground/Pages/扩展计算属性.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 扩展计算属性。注意只能扩展计算属性。 存储属性一旦定义好,就无法再增加。 2 | 3 | //: - 给Double类型扩展计算属性: 4 | 5 | //米为基准 6 | extension Double { 7 | var km: Double { return self / 1_000.0 } 8 | var m: Double { return self } 9 | var cm: Double { return self * 100.0 } 10 | 11 | } 12 | 13 | var dailyRunning = 1000.0 14 | dailyRunning.km 15 | 16 | var mountainEverest = 8848.8 17 | mountainEverest.km 18 | 19 | var height = 1.72 20 | height.cm 21 | 22 | //: [>](@next) 23 | -------------------------------------------------------------------------------- /14.扩展.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /14.扩展.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /14.扩展.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/14.扩展.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /14.扩展.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/14.扩展.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /15.泛型.playground/Pages/使用泛型前.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 使用泛型前 2 | //: - 🌰 打印不同类型的数组 3 | let topLangs = ["Swift", "Kotlin", "Javascript"] 4 | let odds = [1, 3, 5] 5 | let machConstants = [3.141, 1.414, 2.718] 6 | 7 | func printString(array:[String]) { 8 | for e in array { 9 | print(e) 10 | } 11 | } 12 | 13 | func printInt(array: [Int]) { 14 | for e in array { 15 | print(e) 16 | } 17 | } 18 | 19 | func printDouble(array: [Double]) { 20 | for e in array { 21 | print(e) 22 | } 23 | } 24 | 25 | //: [>](@next) 26 | -------------------------------------------------------------------------------- /15.泛型.playground/Pages/关联类型的约束(选修).xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 关联类型的约束: 对一个关联类型进行协议遵从性或类型一致性检查,在参数列表后紧跟where判断语句. 2 | protocol 容器 { 3 | //未指定是谁的别名, 留给遵从此协议的类型提供 4 | associatedtype ItemType 5 | 6 | //别名也用于方法和属性,推测参数或返回值类型 7 | mutating func append(_ item: ItemType) 8 | var count: Int { get } 9 | subscript(i: Int) -> ItemType { get } 10 | } 11 | 12 | func 完全匹配(_ 容器1: C1, _ 容器2: C2) -> Bool where C1.ItemType == C2.ItemType, C1.ItemType: Equatable { 13 | //因为完全匹配算法,只要有一个元素不相等,运算就结束,所以优先筛选出不相等的条件出来 14 | 15 | if 容器1.count != 容器2.count { 16 | return false 17 | } 18 | 19 | for i in 0..<容器1.count where 容器1[i] != 容器2[i] { 20 | return false 21 | } 22 | 23 | return true 24 | } 25 | 26 | extension Array: 容器{} 27 | 28 | var array1 = [1,2,3] 29 | var array2 = [Int]() 30 | array2.append(1) 31 | array2.append(2) 32 | array2.append(3) 33 | 34 | 完全匹配(array2, array1) 35 | 36 | 37 | -------------------------------------------------------------------------------- /15.泛型.playground/Pages/关联类型(选修).xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 关联类型:定义协议时,有时需要定义一个或多个泛型作辅助,用关键字associatedtype指定 2 | protocol 容器 { 3 | //提供给遵从此协议的类型使用 4 | associatedtype ItemType 5 | 6 | //可用于方法和属性,推测参数或返回值类型 7 | mutating func append(_ item: ItemType) 8 | var count: Int { get } 9 | subscript(i: Int) -> ItemType { get } 10 | } 11 | 12 | struct IntStack : 容器{ 13 | var items = [Int]() 14 | 15 | mutating func push(item: Int) { 16 | items.append(item) 17 | } 18 | 19 | mutating func pop() ->Int { 20 | return items.removeLast() 21 | } 22 | 23 | mutating func append(_ item: Int) { 24 | self.push(item: item) 25 | } 26 | 27 | 28 | var count: Int { 29 | return items.count 30 | } 31 | 32 | subscript(i: Int) -> Int { 33 | return items[i] 34 | } 35 | } 36 | 37 | //Element自动推测 38 | struct Stack: 容器 { 39 | var items = [Element]() 40 | 41 | mutating func push(item: Element) { 42 | items.append(item) 43 | } 44 | 45 | mutating func pop() -> Element { 46 | return items.removeLast() 47 | } 48 | 49 | mutating func append(_ item: Element) { 50 | self.push(item: item) 51 | } 52 | 53 | var count: Int { 54 | return items.count 55 | } 56 | 57 | subscript(i:Int) -> Element { 58 | return items[i] 59 | } 60 | } 61 | 62 | //: - 扩展一个既有类型来详述关联类型. 63 | //🌰Swift的数组已实现append,count,下标, 可以直接遵从容器协议. 64 | extension Array: 容器 {} 65 | 66 | 67 | 68 | //: [关联类型的约束](@next) 69 | -------------------------------------------------------------------------------- /15.泛型.playground/Pages/定义.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ---- 2 | //: - [小波说雨燕 3天学会Swift 4](http://www.xiaoboswift.com) 3 | //: ---- 4 | //: 泛型:“泛”指通用、普遍。现代编程必不可少的组成部分。 5 | 6 | //: - 💡数组和字典都是“泛型”集合. 可以是Int, String, 或者任何其它任何类型. 无任何类型的限制. 7 | 8 | //: - 💡print函数可以打印任意类型. 9 | 10 | //: 🤔我写的函数/类型等, 能否也”泛型化“? 11 | 12 | //: [>](@next) 13 | 14 | -------------------------------------------------------------------------------- /15.泛型.playground/Pages/泛型函数.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 泛型函数: 让参数由单一类型,扩展成“广泛的”类型 2 | 3 | //: - 用一个占位符,取代原具体类型,函数名后紧跟<占位符>。占位符可以是任意变量名。 4 | 5 | //打印任意类型数组的全部元素 6 | func printAny(array: [placeHolder]) { 7 | for element in array { 8 | print(element) 9 | } 10 | } 11 | 12 | printAny(array: [1, 3, 5]) 13 | printAny(array: [3.141, 1.414, 2.718]) 14 | printAny(array: ["Swift", "Kotlin", "Javascript"]) 15 | 16 | //: [>](@next) 17 | -------------------------------------------------------------------------------- /15.泛型.playground/Pages/泛型参数.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 泛型参数: 上述例子中的占位符, 作为取代类型的参数使用. 2 | //: - 泛型参数紧跟函数名之后,用尖括号包含,如: <类型占位符>. 3 | //: - 一旦定义了类型参数, 即可用作 参数类型/函数返回类型/函数体中做类型标识. 4 | //: - 无论何时函数被调用时, 类型参数就被实际的类型取代. 可以定义任何多类型参数, <占位符1,占位符2,..> 5 | //: - 命名:尽量顾名思义. 比如 Dictionary , Array. 如无明显关联, 可用如T、U、V等替代. 6 | 7 | func printAny(array: [T]) { 8 | for element in array { 9 | print(element) 10 | } 11 | } 12 | 13 | //: [>](@next) 14 | -------------------------------------------------------------------------------- /15.泛型.playground/Pages/泛型类型.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 泛型类型: 泛型不仅用于函数,也可以用于类型定义中。比如,一个适应任何类型参数的类/结构体/枚举等, 像数组和字典一样. 2 | //: - 🌰创建一个类型: Stack(堆). 3 | //: - 堆使用规则: 后进(push)先出(pop) 4 | //💡压一个值进堆顶,作为后进的值。 5 | //💡堆顶的值可以被推出。 6 | 7 | //Int堆 8 | struct IntStack { 9 | var items = [Int]() 10 | 11 | mutating func push(item: Int) { 12 | items.append(item) 13 | } 14 | 15 | mutating func pop() ->Int { 16 | return items.removeLast() 17 | } 18 | } 19 | 20 | //泛型Stack 21 | struct Stack { 22 | var items = [T]() 23 | 24 | mutating func push(item: T) { 25 | items.append(item) 26 | } 27 | 28 | mutating func pop() -> T { 29 | return items.removeLast() 30 | } 31 | } 32 | 33 | var topLang = Stack() 34 | topLang.push(item: "Swift") 35 | topLang.push(item: "Kotlin") 36 | topLang.push(item: "Javascript") 37 | topLang.push(item: "Python") 38 | 39 | let thisYearTop = topLang.pop() 40 | 41 | topLang 42 | 43 | //作业:创建一个自定义的泛型类型(30分钟) 44 | 45 | //: [>](@next) 46 | -------------------------------------------------------------------------------- /15.泛型.playground/Pages/泛型类型.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 11 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /15.泛型.playground/Pages/泛型约束(选修).xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 泛型约束: 有时候需要强制泛型在一定的范围内, 比如是某个类的子类,或遵从某个协议. 2 | //func 某函数(某参数: T, 某类型: U) { 3 | // //函数体 4 | //} 5 | 6 | //Equatable协议,顾名思义,可作相等运算的 7 | func 查位置(文本数组:[Element], 待查元素:Element) -> Int? { 8 | for (索引, 值) in 文本数组.enumerated() { 9 | if 值 == 待查元素 { 10 | return 索引 + 1 11 | } 12 | } 13 | return nil 14 | } 15 | 16 | 17 | 查位置(文本数组: ["刘备","关羽","张飞","孔明"], 待查元素: "关羽") 18 | //: [关联类型](@next) 19 | -------------------------------------------------------------------------------- /15.泛型.playground/Resources/pushpop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/15.泛型.playground/Resources/pushpop.png -------------------------------------------------------------------------------- /15.泛型.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /15.泛型.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /15.泛型.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/15.泛型.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /15.泛型.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/15.泛型.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /15.泛型.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState_20160414124711.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/15.泛型.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState_20160414124711.xcuserstate -------------------------------------------------------------------------------- /15.泛型.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState_20160414124718.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/15.泛型.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState_20160414124718.xcuserstate -------------------------------------------------------------------------------- /2.基础操作符.playground/Pages/操作符简介.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ---- 2 | //: - [小波说雨燕 3天学会Swift 4](http://www.xiaoboswift.com) 3 | //: - 课件:[github.com/yagamis](http://github.com/yagamis/swift2basic) 4 | //: ---- 5 | //: 基础操作符 6 | //: 7 | //: ---- 8 | //: 9 | //: - 一个符号或单词,类似数学中的 + - * / 10 | //: 11 | //: [>](@next) 12 | -------------------------------------------------------------------------------- /2.基础操作符.playground/Pages/数学操作符.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 数学操作符 : + - * / % 2 | 3 | 1 + 2 4 | 3 - 4 5 | 5 * 6 6 | 12 / 3 7 | 9 % 4 8 | 9 | //求余示意图 10 | demo() 11 | 12 | 13 | //: [>](@next) 14 | -------------------------------------------------------------------------------- /2.基础操作符.playground/Pages/数学操作符.xcplaygroundpage/Resources/remainderInteger_2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/2.基础操作符.playground/Pages/数学操作符.xcplaygroundpage/Resources/remainderInteger_2x.png -------------------------------------------------------------------------------- /2.基础操作符.playground/Pages/数学操作符.xcplaygroundpage/Sources/showImage.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | import Foundation 3 | 4 | public func demo() -> UIImage { 5 | return UIImage(named: "remainderInteger_2x")! 6 | } 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /2.基础操作符.playground/Pages/数学操作符.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /2.基础操作符.playground/Pages/术语.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 术语 2 | //: - 操作几个对象,被称之为几元操作符 3 | //: - 操作一个目标, 是一元操作符. 4 | var a = 6 5 | let b = -a 6 | 7 | 8 | //: - 二元操作符操作2个目标,在2个目标之间,前后用空格分隔. 9 | 3 + 4 10 | 11 | 12 | 13 | //: [赋值操作符>](@next) 14 | -------------------------------------------------------------------------------- /2.基础操作符.playground/Pages/比较操作符.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 比较操作符: > >= < <= . 结果是true或false. 2 | 1 > 2 3 | 1 >= 1 4 | 3 < 2 5 | 3 <= 3 6 | 3 == 3 7 | 8 | //: [>](@next) 9 | -------------------------------------------------------------------------------- /2.基础操作符.playground/Pages/组合赋值操作符.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 组合赋值: 赋值与数学操作符的组合,比如 += 2 | var a = 1 3 | a += 2 4 | a -= 1 5 | a *= 4 6 | a /= 2 7 | 8 | 9 | //: [>](@next) 10 | -------------------------------------------------------------------------------- /2.基础操作符.playground/Pages/赋值操作符.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 赋值操作符 2 | //: - 赋予或更新一个变量。 3 | var a = 3 4 | 5 | a = 5 6 | 7 | 8 | //: [>](@next) 9 | -------------------------------------------------------------------------------- /2.基础操作符.playground/Pages/逻辑操作符.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 逻辑操作符 : 用于表示逻辑中的"与"、"或"、"非" 2 | //: - 与: 每项全部为true, 整体为true 3 | true && true && true 4 | //: - 或: 有一项为true, 整体为true 5 | true || false || false 6 | 7 | //: - 非: 对Bool值取反 8 | let a = false 9 | !a 10 | -------------------------------------------------------------------------------- /2.基础操作符.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /2.基础操作符.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /2.基础操作符.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/2.基础操作符.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /2.基础操作符.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | BuildLocationStyle 6 | UseAppPreferences 7 | CustomBuildLocationType 8 | RelativeToDerivedData 9 | DerivedDataLocationStyle 10 | Default 11 | IssueFilterStyle 12 | ShowActiveSchemeOnly 13 | LiveSourceIssuesEnabled 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /2.基础操作符.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/2.基础操作符.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /3.字符串和字符.playground/Pages/多行字符串.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 多行字符串。💡Swift 4 新功能 2 | let name = """ 3 | 4 | 10 | js演示 11 | 12 | 13 |

14 | 这是一个swift多行字符串 15 |

16 | 17 | 18 | 19 | 20 | """ 21 | 22 | 23 | //: [>](@next) 24 | 25 | -------------------------------------------------------------------------------- /3.字符串和字符.playground/Pages/字符.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 字符 2 | //: - 用Character类型来定义字符串. 3 | var eat: Character = "吃" 4 | var chicken: Character = "鸡" 5 | 6 | //: [>](@next) 7 | -------------------------------------------------------------------------------- /3.字符串和字符.playground/Pages/字符串字面量.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 字符串字面量. 包含双引号""的值成为字符串字面量. 2 | "大吉大利" 3 | 4 | //: - 空字符串: 用""字面量 5 | var a = "" 6 | var b = " " 7 | //: - 判断字符串是否为空 8 | a.isEmpty 9 | b.isEmpty 10 | 11 | //: - 字符串长度 12 | b.count 13 | 14 | //: [>](@next) 15 | -------------------------------------------------------------------------------- /3.字符串和字符.playground/Pages/字符串插值.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 字符串插值:把常量/变量/字面量/表达式插入到字符串形成一个模板。 2 | //: 所以也叫字符串模板(Kotlin)。 3 | 4 | let name = "解放大官人" 5 | let platform = "斗鱼" 6 | let rank = 1 7 | let game = "吃鸡" 8 | let price = 380 9 | 10 | let msg = """ 11 | \(name)在\(platform)\(game)排名第\(rank)。 12 | """ 13 | 14 | 15 | //: [>](@next) 16 | -------------------------------------------------------------------------------- /3.字符串和字符.playground/Pages/字符串插值.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /3.字符串和字符.playground/Pages/字符串索引(选修).xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 修改字符串. 通过字符串的各种方法和属性,使用索引最为便捷. 2 | 3 | //: - 字符串索引: 索引对应其中每一个字符的位置. 4 | let a = """ 5 | 海客谈瀛洲 6 | 烟涛微茫信难求 7 | 越人语天姥 8 | 云霞明灭或可睹 9 | 天姥连天向天横 10 | 势拔五岳掩赤城 11 | 天台一万八千丈 12 | 对此欲倒东南倾 13 | """ 14 | 15 | //: - 首字符索引 startIndex, endIndex是尾字符后一个位置. 16 | a.startIndex 17 | a.endIndex 18 | 19 | //: - 确定索引后,用下标来访问相应字符 20 | a[a.startIndex] 21 | 22 | //: - 用字符串的index(after:)或index(before)引用后一个或前一个索引 23 | a[a.index(after: a.startIndex)] 24 | a[a.index(before: a.endIndex)] 25 | 26 | //: - 用字符串的index(_:offsetBy:)方法向前进位 27 | a[a.index(a.startIndex, offsetBy: 3)] 28 | 29 | //: - 字符串的indices属性表示索引的区间. 30 | for index in a.indices { 31 | print(a[index]) 32 | } 33 | 34 | //: [>](@next) 35 | -------------------------------------------------------------------------------- /3.字符串和字符.playground/Pages/插入和删除(选修).xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 修改字符串-插入和删除 2 | var a = "大吉大利" 3 | //: - 插入一个字符 4 | a.insert("、", at: a.endIndex) 5 | 6 | //: - 插入一个字符串 7 | let b = "今晚吃鸡。" 8 | a.insert(contentsOf: b, at: a.endIndex) 9 | 10 | 11 | //: - 删除指定位置的字符,用removeAtIndex方法.🌰删除字符串的最后一位. 12 | a.remove(at: a.index(before: a.endIndex)) 13 | 14 | 15 | //: - 删除一个范围的子串,用removeRange方法.🌰删除之前添加的字符串 16 | let start = a.index(a.endIndex, offsetBy: -(b.count)) 17 | let subRange = start..](@next) 25 | -------------------------------------------------------------------------------- /3.字符串和字符.playground/Pages/比较字符串(选修).xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 比较字符串: 字符串和字符相等性,前缀相等,后缀相等 2 | 3 | //: - 字符串和字符相等性:用 == 或 != 操作符. Unicode扩展语素组合而成的字符与正式的单个字符被视为相等. 4 | let x = "\u{1112}\u{1161}\u{11ab}" 5 | 6 | "\u{1112}" 7 | "\u{1161}" 8 | "\u{11ab}" 9 | 10 | 11 | let y = "\u{d55c}" 12 | 13 | x == y 14 | 15 | 16 | //: - 但不同语义的Unicode字符是不相等的. 比如英语的A和俄文的A虽然相似,但语义不同,不能视为相等. 中文的"对"和日文的"対"虽然语义相同,字型明显不同. 17 | 18 | 19 | //: - 前缀和后缀相等: 用于检查一个字符串是否具有特定前缀和后缀. hasPrefix和hasSuffix 20 | let a = "等额本息" 21 | let b = "等额本金!" 22 | 23 | let c = "等额" 24 | let d = "!" 25 | 26 | a.hasPrefix(c) 27 | b.hasPrefix(c) 28 | 29 | a.hasSuffix(d) 30 | b.hasSuffix(d) 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /3.字符串和字符.playground/Pages/特殊字符.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: - 特殊字符: 常用半角控制字符(单斜杠 换行 单引号 双引号) 2 | 3 | print("\\ \n \" \' ") 4 | 5 | //: - Unicode: 万国码,可以使用Unicode标量表示各种字符:\u{} 。[汉字Unicode一览](http://www.asahi-net.or.jp/~ax2s-kmtn/ref/unicode/cjku_klist.html) 6 | "\u{6c49}" 7 | "\u{5b57}" 8 | 9 | 10 | 11 | //: [>](@next) 12 | -------------------------------------------------------------------------------- /3.字符串和字符.playground/Pages/简介.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ---- 2 | //: - [小波说雨燕 3天学会Swift 4](http://www.xiaoboswift.com) 3 | //: - 全集课件:[github.com/yagamis](http://github.com/yagamis/swift2basic) 4 | //: ---- 5 | //: 字符串和字符 6 | //: 7 | //: ---- 8 | //: 9 | //: - 字符串(String)是字符(Character)的集合,表示一段文本。 10 | //: - 字符串可以通过 + 来连接. 11 | //: - 💡通过字符串插值可把变量插入到字符串中. 12 | //: 13 | //: ---- 14 | //: 15 | //: [>](@next) 16 | -------------------------------------------------------------------------------- /3.字符串和字符.playground/Pages/连接字符串和字符.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 连接字符串和字符 2 | //: - 用 + 3 | let good1 = "大吉" 4 | let good2 = "大利" 5 | 6 | var good = good1 + good2 7 | 8 | //: - 向字符串添加字符, 用append方法 9 | let dinner = " 今晚吃鸡😆" 10 | 11 | good.append(dinner) 12 | 13 | 14 | 15 | //: [>](@next) 16 | -------------------------------------------------------------------------------- /3.字符串和字符.playground/Pages/连接字符串和字符.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /3.字符串和字符.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /3.字符串和字符.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /3.字符串和字符.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/3.字符串和字符.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /3.字符串和字符.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | BuildLocationStyle 6 | UseAppPreferences 7 | CustomBuildLocationType 8 | RelativeToDerivedData 9 | DerivedDataLocationStyle 10 | Default 11 | IssueFilterStyle 12 | ShowActiveSchemeOnly 13 | LiveSourceIssuesEnabled 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /3.字符串和字符.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/3.字符串和字符.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /4.集合类型.playground/Pages/Set.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 集合(Set): 值无序不重复. 适合存储具有唯一性的数据, 如用户名/身份证号码/护照号等 2 | var idcards : Set = [123,456,999,888,123,456,123] 3 | 4 | 5 | //: - 用数组字面量创建集合 6 | 7 | 8 | //: - ①元素数: count, 空否: isEmpty 9 | idcards.count 10 | idcards.isEmpty 11 | 12 | //: - ②插入:insert 13 | idcards.insert(999) 14 | 15 | //: - ③移除:remove 16 | idcards.remove(999) 17 | 18 | //: - ④是否包含某元素:contains 19 | idcards.contains(123) 20 | 21 | //: - ⑤转换为数组:sorted 22 | let idcardsArray = idcards.sorted() 23 | 24 | 25 | //: - 集合间的运算: 交差并补(选修) 26 | setdemo() 27 | 28 | 29 | 30 | //: - 交集 intersection 31 | var x: Set = [1,2,3,4] 32 | let y: Set = [3,4,5,6] 33 | 34 | x.intersection(y) 35 | 36 | 37 | //: - 补集 symmetricDifference 38 | x.symmetricDifference(y) 39 | 40 | //: - 差集 subtract 41 | x.subtract(y) 42 | 43 | 44 | 45 | //: - 并集 union 46 | x.union(y) 47 | 48 | 49 | //: - 集合间的关系 50 | setRelation() 51 | 52 | 53 | 54 | //: - 💡相等: 两个集合有完全相同的元素. 55 | let i : Set = [1,2,3] 56 | let j : Set = [1,2,3,4] 57 | 58 | 59 | //: - ①子集: isSubset(可以相等), 严格子集isStrictSubset 60 | i.isSubset(of: j) 61 | i.isStrictSubset(of: j) 62 | 63 | //: - ②父集: isSupersetOf(可以相等), 严格父集isStrictSuperSetOf 64 | j.isSuperset(of: i) 65 | j.isStrictSuperset(of: i) 66 | 67 | //: - ③无交集: isDisjoint 68 | let youfav: Set = ["game","comics"] 69 | let girlfav: Set = ["shopping","eat"] 70 | 71 | youfav.isDisjoint(with: girlfav) 72 | //: [>](@next) 73 | -------------------------------------------------------------------------------- /4.集合类型.playground/Pages/Set.xcplaygroundpage/Resources/setEulerDiagram_2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/4.集合类型.playground/Pages/Set.xcplaygroundpage/Resources/setEulerDiagram_2x.png -------------------------------------------------------------------------------- /4.集合类型.playground/Pages/Set.xcplaygroundpage/Resources/setVennDiagram_2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/4.集合类型.playground/Pages/Set.xcplaygroundpage/Resources/setVennDiagram_2x.png -------------------------------------------------------------------------------- /4.集合类型.playground/Pages/Set.xcplaygroundpage/Sources/showSet.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | import UIKit 4 | 5 | 6 | public func setdemo() -> UIImage? { 7 | return UIImage(named: "setVennDiagram_2x") 8 | } 9 | 10 | public func setRelation() -> UIImage? { 11 | return UIImage(named: "setEulerDiagram_2x") 12 | } 13 | -------------------------------------------------------------------------------- /4.集合类型.playground/Pages/Set.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 11 | 16 | 17 | 21 | 22 | 26 | 27 | 31 | 32 | 37 | 38 | 42 | 43 | 47 | 48 | 52 | 53 | 58 | 59 | 63 | 64 | 68 | 69 | 74 | 75 | 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /4.集合类型.playground/Pages/字典.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 字典: 值无序可重复,但每个值有唯一的键(Key) 2 | 3 | //: - 用字典字面量来创建字典, 键与值之间用冒号隔开. 4 | var airports = ["PVG":"上海浦东国际机场", "CHU":"周水子机场", "DUB":"Dublin Airport"] 5 | 6 | //: - ①计数: count, 空否: isEmpty 7 | airports.count 8 | airports.isEmpty 9 | 10 | 11 | //: - ②添加或更新: 字典变量[键] = 值 12 | airports["SHQ"] = "虹桥机场" 13 | airports["NKG"] = "禄口机场" 14 | 15 | 16 | //: - ③获取,依然可以用下标 17 | airports["NKG"] 18 | 19 | 20 | //: - ④移除,用下标把值设为nil 21 | airports["NKG"] = nil 22 | 23 | 24 | //: - 把键值分离成数组,用 [数组类型] (字典变量.keys), [数组类型] (字典变量.values) 25 | let codes = [String](airports.keys) 26 | let names = [String](airports.values) 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /4.集合类型.playground/Pages/字典.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 15 | 16 | 20 | 21 | 25 | 26 | 30 | 31 | 36 | 37 | 42 | 43 | 48 | 49 | 53 | 54 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /4.集合类型.playground/Pages/数组.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 数组 2 | //: - 有序可重复,用于存储同一类型的值. 3 | 4 | //: - 创建一个有默认值的数组 5 | let array1 = Array(repeating: "小姐姐", count: 30) 6 | 7 | //: - ⚡️创建一个整数区间内元素的数组,Array(区间) 8 | let intRangeArray = Array(-10...10) 9 | 10 | //: - ⚡️💡用数组字面量来创建数组: [值1, 值2, ..., 值N] 11 | var topLangs = ["Swift","Python","Kotlin","JavaScript"] 12 | 13 | 14 | //: - ①元素数:count, 空否: isEmpty 15 | topLangs.count 16 | topLangs.isEmpty 17 | 18 | 19 | //: - ②➕添加: append, 添加一个同类型数组 += 20 | topLangs.append("php") 21 | let h5 = ["HTML","CSS"] 22 | topLangs += h5 23 | 24 | //: - ③获取元素 : 数组名[索引], ⚠️索引总是从0开始 25 | topLangs[5] 26 | 27 | 28 | //: - ④插入: insert 29 | topLangs.insert("React Native", at: 1) 30 | 31 | //: - ⑤移除: remove 32 | topLangs.remove(at: 1) 33 | 34 | 35 | //: - 高阶操作①: 求和,连接元素, reduce 36 | let topLangsTitle = topLangs.reduce("我最喜爱的编程语言:") { 37 | $0 + " " + $1 38 | } 39 | 40 | let numbers = [1,2,3,100] 41 | let sum = numbers.reduce(0) { 42 | $0 + $1 43 | } 44 | //: - 高阶操作②: 变形 map 45 | let numbersToStr = numbers.map { (number) -> String in 46 | return "\(number)" 47 | } 48 | 49 | 50 | 51 | 52 | //: [>](@next) 53 | -------------------------------------------------------------------------------- /4.集合类型.playground/Pages/数组.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 15 | 16 | 21 | 22 | 27 | 28 | 32 | 33 | 37 | 38 | 42 | 43 | 47 | 48 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /4.集合类型.playground/Pages/简介.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ---- 2 | //: - 小波说雨燕 3天学会Swift 4 3 | //: - 全集课件:[github.com/yagamis](http://github.com/yagamis/swift2basic) 4 | //: ---- 5 | 6 | //: 集合类型 7 | //: 8 | //: ---- 9 | //: - 一组同类型的值的组合, 根据组合的整体特性分为: 10 | //: - 有序可重复 - 数组(Array) 11 | //: - 无序不重复 - Set 12 | //: - 无序可重复,但每个值有唯一的键(Key) - 字典(Dictionary) 13 | //: - ☆批量处理集合中的元素, 可以使用 for in循环 14 | demo() 15 | 16 | 17 | 18 | //: [>](@next) 19 | -------------------------------------------------------------------------------- /4.集合类型.playground/Pages/简介.xcplaygroundpage/Resources/CollectionTypes_intro_2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/4.集合类型.playground/Pages/简介.xcplaygroundpage/Resources/CollectionTypes_intro_2x.png -------------------------------------------------------------------------------- /4.集合类型.playground/Pages/简介.xcplaygroundpage/Sources/showImage.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | public func demo() -> UIImage?{ 4 | return UIImage(named: "CollectionTypes_intro_2x") 5 | } 6 | -------------------------------------------------------------------------------- /4.集合类型.playground/Pages/简介.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 11 | 15 | 16 | 20 | 21 | 25 | 26 | 30 | 31 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /4.集合类型.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /4.集合类型.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /4.集合类型.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/4.集合类型.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /4.集合类型.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | BuildLocationStyle 6 | UseAppPreferences 7 | CustomBuildLocationType 8 | RelativeToDerivedData 9 | DerivedDataLocationStyle 10 | Default 11 | IssueFilterStyle 12 | ShowActiveSchemeOnly 13 | LiveSourceIssuesEnabled 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /4.集合类型.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/4.集合类型.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /5.控制流.playground/Pages/for循环.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: for-in循环 2 | //: 操作序列(如数组、集合、字典)中的每一项,或执行特定次数的循环操作。 3 | 4 | for i in 1...5 { 5 | print(i) 6 | } 7 | 8 | //: - 序列中的每项都参与. for与in之间加一个代替每项的名. 9 | let cities = ["北","上","广"] 10 | for city in cities { 11 | print(city) 12 | } 13 | 14 | var airports = ["PVG":"浦东机场", "CHU":"周水子机场", "DUB":"Dublin Airport"] 15 | //: - 循环一个字典, 因为键值对有2个元素, 用元组变量. 16 | for (key,value) in airports { 17 | print(key,value) 18 | } 19 | 20 | //: - 单独使用其中键或值,使用 keys 或 values 21 | for value in airports.values { 22 | print(value) 23 | } 24 | 25 | 26 | //: [>](@next) 27 | -------------------------------------------------------------------------------- /5.控制流.playground/Pages/if和循环控制.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 根据条件做不同的分支处理.if else 2 | var temp = 25 3 | 4 | if temp > 28 { 5 | print("开空调") 6 | } else { 7 | print("关空调") 8 | } 9 | 10 | //: 循环中的判断 11 | //: - continue 结束本次循环, break 结束整个循环 12 | for i in 1...100 { 13 | if i == 3 { 14 | break 15 | } 16 | print(i) 17 | } 18 | 19 | //: [>](@next) 20 | -------------------------------------------------------------------------------- /5.控制流.playground/Pages/if和循环控制.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /5.控制流.playground/Pages/switch.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: - switch语句:超过2个以上条件判断的场合,支持各种匹配条件。 2 | 3 | //: - 1⃣️范围匹配:如根据室外温度来自动调节室内温度的空调 4 | 5 | var temp = 39 6 | 7 | switch temp { 8 | case 35...42: 9 | print("强力制冷") 10 | case 25...34: 11 | print("Normal cool") 12 | case 18...24: 13 | print("wet fitler") 14 | case 0...17: 15 | print("warm mode") 16 | default: 17 | print("not working") 18 | } 19 | 20 | 21 | //: - 2⃣️元组匹配(选修):如某坐标(含x和y两个变量)所在象限判断. 22 | 坐标图1() 23 | 24 | let point1 = (2,-2) 25 | 26 | switch point1 { 27 | case (0, 0): 28 | print("zero point") 29 | case (_, 0): 30 | print("x axis") 31 | case (0, _): 32 | print("y axis") 33 | case (-2...2, -2...2): 34 | print("located in 2 * 2 rectangle ") 35 | default: 36 | break 37 | } 38 | 39 | 40 | //: - 3⃣️值绑定(选修):一个case里可以绑定临时变量,以便在执行语句中使用. 41 | //把坐标的值显示出来 42 | switch point1 { 43 | case (let x, 0): 44 | print("x is", x) 45 | case (let x, let y): 46 | print(x,y) 47 | } 48 | 49 | //: - 4⃣️值绑定的where再匹配(选修): 50 | 51 | 52 | //判断坐标是否在对角线上 53 | switch point1 { 54 | case (let x, 0): 55 | print("x is", x) 56 | case (let x, let y) where x == y: 57 | print("45对角线",x,y) 58 | case (let x, let y) where x != y: 59 | print("非对角线",x,y) 60 | 61 | default: 62 | break 63 | } 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /5.控制流.playground/Pages/switch.xcplaygroundpage/Sources/New File.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | public func 坐标图1() -> UIImage? { 4 | return UIImage(named: "coordinate") 5 | } 6 | 7 | public func 坐标图2() -> UIImage? { 8 | return UIImage(named: "coordinate-2") 9 | } 10 | -------------------------------------------------------------------------------- /5.控制流.playground/Pages/switch.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 11 | 16 | 17 | 21 | 22 | 27 | 28 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /5.控制流.playground/Pages/while循环(选修).xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: while循环执行一系列操作,直到条件不成立. 2 | //: 使用场合:执行次数未知。💡开发中极少使用。 3 | 4 | //: - 格式:while 条件 { 语句 }. 比如计算1到100的和。 5 | 6 | var result = 0 7 | let targetResult = 5050 8 | var initNumber = 1 9 | 10 | while result < targetResult { 11 | result += initNumber 12 | initNumber += 1 13 | } 14 | print(initNumber) 15 | 16 | 17 | 18 | 19 | //: [>](@next) 20 | -------------------------------------------------------------------------------- /5.控制流.playground/Pages/while循环(选修).xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 15 | 16 | 21 | 22 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /5.控制流.playground/Pages/早退语句(选修).xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 早退语句: 在必需条件不满足的早期阶段退出执行. 2 | //: 语句关键字 guard, 与if相似. 属于条件强调意味很浓的语句. 3 | //: guard let 与 if let相似,用于可选类型. guard有助于提升代码可读性. 4 | var isOrderFromApp = true 5 | var credit : Int? = 7 6 | 7 | func callTaxi() { 8 | guard isOrderFromApp else { 9 | return 10 | } 11 | 12 | guard let mycredit = credit , mycredit >= 6 else{ 13 | return 14 | } 15 | 16 | print("打车成功!") 17 | } 18 | 19 | callTaxi() 20 | 21 | //: [>](@next) 22 | -------------------------------------------------------------------------------- /5.控制流.playground/Pages/简介.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 控制流 2 | //: 3 | //: ---- 4 | //: - 根据业务逻辑来控制运行流向 5 | //: - for循环 6 | //: - while循环(选修) 7 | //: - if和循环控制 8 | //: - 早退语句(选修) 9 | //: - switch 10 | //: 11 | //: ---- 12 | //: 13 | 14 | 15 | 16 | //: [>](@next) 17 | -------------------------------------------------------------------------------- /5.控制流.playground/Resources/coordinate-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/5.控制流.playground/Resources/coordinate-2.png -------------------------------------------------------------------------------- /5.控制流.playground/Resources/coordinate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/5.控制流.playground/Resources/coordinate.png -------------------------------------------------------------------------------- /5.控制流.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /5.控制流.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /5.控制流.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/5.控制流.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /5.控制流.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | BuildLocationStyle 6 | UseAppPreferences 7 | CustomBuildLocationType 8 | RelativeToDerivedData 9 | DerivedDataLocationStyle 10 | Default 11 | IssueFilterStyle 12 | ShowActiveSchemeOnly 13 | LiveSourceIssuesEnabled 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /5.控制流.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/5.控制流.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /6.函数、闭包和高阶函数.playground/Pages/参数和返回值.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 参数和返回值 2 | 3 | //: - 无参数无返回值 4 | func welcome1() { 5 | print("Welcome") 6 | print("learn Swift 4!") 7 | } 8 | 9 | //: - 多返回值(使用元组) 10 | func maxMin() -> (Int, Double) { 11 | return (Int.max, 0.0) 12 | } 13 | 14 | //: - 可以给某个参数以默认值 15 | func add2(x: Int, increment: Int = 2) -> Int { 16 | return x + increment 17 | } 18 | 19 | //★练习:使用变量式定义来改写上述函数 20 | 21 | 22 | //: [>](@next) 23 | -------------------------------------------------------------------------------- /6.函数、闭包和高阶函数.playground/Pages/定义函数.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 定义和使用函数 2 | //: 常规式定义 3 | func add(x: Int, y: Int) -> Int { 4 | return x + y 5 | } 6 | 7 | var a = add(x: 1, y: 2) 8 | let b = add(x: -1, y: -2) 9 | 10 | 11 | //: ★变量式定义,也叫闭包表达式 12 | var jiafa = { (x: Int, y: Int) -> Int in 13 | return x + y 14 | } 15 | 16 | jiafa(1,2) 17 | 18 | 19 | //: [>](@next) 20 | -------------------------------------------------------------------------------- /6.函数、闭包和高阶函数.playground/Pages/简介.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ---- 2 | //: - 小波说雨燕 3天学会Swift 4 3 | //: - 全集课件:[github.com/yagamis](http://github.com/yagamis/swift2basic) 4 | //: ---- 5 | //: 函数 6 | //: 7 | //: ---- 8 | //: - 一段代码块,实现一个特定的功能,比如print函数。 9 | //: - 有的函数返回一个结果,就像公式一样。 10 | //: - 使用函数的“动词”有:call,invoke,perform,execute 11 | //: ---- 12 | //: 13 | 14 | 15 | 16 | //: [>](@next) 17 | -------------------------------------------------------------------------------- /6.函数、闭包和高阶函数.playground/Pages/高阶函数.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 既然函数可以写成变量,那也可以当类型来使用。 2 | func jisuan(x: Int, y: Int, method: (Int,Int) -> Int) -> Int { 3 | return method(x, y) 4 | } 5 | 6 | func add(x: Int, y: Int) -> Int { 7 | return x + y 8 | } 9 | 10 | func multipy(x : Int, y: Int) -> Int { 11 | return x * y 12 | } 13 | 14 | let z = jisuan(x: 9, y: 3, method: add) 15 | let u = jisuan(x: 9, y: 3, method: multipy) 16 | 17 | 18 | //闭包写法 19 | let b = jisuan(x: 9, y: 3) { (x, y) -> Int in 20 | return x * y 21 | } 22 | 23 | //闭包中的默认参数名 24 | let c = jisuan(x: 9, y: 3) { $0 / $1 } 25 | -------------------------------------------------------------------------------- /6.函数、闭包和高阶函数.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /6.函数、闭包和高阶函数.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /6.函数、闭包和高阶函数.playground/playground.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | BuildSystemType 6 | Latest 7 | 8 | 9 | -------------------------------------------------------------------------------- /6.函数、闭包和高阶函数.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/6.函数、闭包和高阶函数.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /6.函数、闭包和高阶函数.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | BuildLocationStyle 6 | UseAppPreferences 7 | CustomBuildLocationType 8 | RelativeToDerivedData 9 | DerivedDataLocationStyle 10 | Default 11 | IssueFilterStyle 12 | ShowActiveSchemeOnly 13 | LiveSourceIssuesEnabled 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /6.函数、闭包和高阶函数.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/6.函数、闭包和高阶函数.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /6.函数、闭包和高阶函数.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | BuildLocationStyle 6 | UseAppPreferences 7 | BuildSystemType 8 | Latest 9 | CustomBuildLocationType 10 | RelativeToDerivedData 11 | DerivedDataLocationStyle 12 | Default 13 | EnabledFullIndexStoreVisibility 14 | 15 | IssueFilterStyle 16 | ShowActiveSchemeOnly 17 | LiveSourceIssuesEnabled 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /7.枚举.playground/Pages/定义和使用.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 定义和使用 : 2 | //: - 给定一个名称, 然后把每一种情况列举出来. 3 | enum Weather { 4 | case sunny, cloudy, rainy, snowy, froggy 5 | } 6 | 7 | 8 | //: - 使用:点语法 9 | let todayWeather = Weather.snowy 10 | 11 | 12 | //: - 与switch配合使用。如果变量是枚举值,可省略枚举名。如果switch语句不列举枚举类型的所有情况,则必须要有default语句来处理其他情况。 13 | switch todayWeather { 14 | case .sunny: 15 | print("艳阳高照,宜出门旅行") 16 | case .cloudy: 17 | print("阴天,回房间吧") 18 | default: 19 | print("天气不对!") 20 | break 21 | } 22 | 23 | //: - 附加值(选修): 每一种类型都可附加一个或多个值,形式是元组。 🌰case 类型(Int,String,...) 24 | enum WeatherDetail { 25 | case lifeIndex(Int, Int, String) 26 | case pmIndex(String, Int) 27 | } 28 | 29 | //: - 赋予附加值:加上元组字面量. 30 | let weatherDetailToday = WeatherDetail.lifeIndex(20, 30, "绥化") 31 | let wetherDetailTomorrow = WeatherDetail.pmIndex("2.5", 120) 32 | 33 | //: - 取得附加值:用switch语句, 在case中加上元组变量. 34 | switch wetherDetailTomorrow { 35 | case .lifeIndex(let uv, let li, let city): 36 | print("紫外线:\(uv),晾晒指数:\(li),推荐出行地: \(city)") 37 | case .pmIndex(let category, let index): 38 | print("PM\(category)指数为:\(index)") 39 | } 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /7.枚举.playground/Pages/定义和使用.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /7.枚举.playground/Pages/简介.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ---- 2 | //: - 小波说雨燕 3天学会Swift 4 3 | //: - 全集课件:[github.com/yagamis](http://github.com/yagamis/swift2basic) 4 | //: ---- 5 | //: 枚举 enumeration 6 | //: 7 | //: ---- 8 | //: - 计数、列举(有限的情况);详细叙述之意。“增强的、更抽象型“的数字类型。 9 | //: - 非常强大,与switch语句结合使用,天生一对。 10 | //: ---- 11 | //: 12 | 13 | 14 | //: [>](@next) 15 | -------------------------------------------------------------------------------- /7.枚举.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /7.枚举.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /7.枚举.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/7.枚举.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /7.枚举.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | BuildLocationStyle 6 | UseAppPreferences 7 | CustomBuildLocationType 8 | RelativeToDerivedData 9 | DerivedDataLocationStyle 10 | Default 11 | IssueFilterStyle 12 | ShowActiveSchemeOnly 13 | LiveSourceIssuesEnabled 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /7.枚举.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/7.枚举.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /7.枚举.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState_20160414134457.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/7.枚举.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState_20160414134457.xcuserstate -------------------------------------------------------------------------------- /8.面向对象.playground/Pages/初始化、构造器.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: - 一个类必须给具体的值才能起作用,这叫初始化或实例化。 2 | //: - 构造器:一个类的初始化方法。 属性:一个类的"特征"。方法:一个类的“行为”。 3 | class Duck { 4 | var color : String 5 | var foot : Int 6 | 7 | init(color: String, foot: Int) { 8 | self.color = color 9 | self.foot = foot 10 | } 11 | 12 | func growUp(color: String) { 13 | self.color = color 14 | } 15 | } 16 | 17 | //生成了一只鸭,叫初始化。duck1是一个实例。 18 | let duck1 = Duck(color: "yellow", foot: 2) 19 | duck1.growUp(color: "white") 20 | 21 | //如果给属性值,会生成一个默认的构造器init(),初始化时不能更改属性值 22 | class DuckGreen { 23 | var color = "Green" 24 | var foot = 2 25 | } 26 | let duck2 = DuckGreen() 27 | 28 | 29 | //结构体只需声明属性,构造器系统自动生成! 30 | struct DuckRed { 31 | var color = "Red" 32 | var foot = 2 33 | } 34 | let duck3 = DuckRed() 35 | let duck4 = DuckRed(color: "Deep Red", foot: 2) 36 | 37 | //: [>](@next) 38 | -------------------------------------------------------------------------------- /8.面向对象.playground/Pages/概念.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ---- 2 | //: - 小波说雨燕 [3天学会Swift 4](http://xiaoboswift.com) 3 | //: ---- 4 | //: - 面向过程式的编程,在1960年左右的大型程序时遇到不少瓶颈和问题。 5 | //: - 此后的高级语言,开始借鉴面向对象的思维方式,发展到今天更加完善。 6 | //: - 面向对象主要从生物的特征、行为、繁殖、代差几个方面来思考。 7 | //: - Swift中, 类(class)、结构体(struct)、 枚举(enum)都有面向对象功能。 8 | 9 | 10 | //: - 比如一只可爱的小黄鸭,有毛色、会划水,长大之后毛色会变。小黄鸭有祖类(野鸭),亲戚类(鸳鸯)。 11 | -------------------------------------------------------------------------------- /8.面向对象.playground/Pages/概念.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /8.面向对象.playground/Pages/继承、重载和计算属性.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 类可以被继承,子类可以修改父类中的属性和方法。结构体和枚举不能继承。 2 | class Duck { 3 | var color : String 4 | var foot : Int 5 | 6 | init(color: String, foot: Int) { 7 | self.color = color 8 | self.foot = foot 9 | } 10 | 11 | func growUp(color: String) { 12 | self.color = color 13 | } 14 | } 15 | 16 | //鸳鸯继承了鸭子的所有属性和方法 17 | class MandarinDuck: Duck { 18 | //增加了属性 19 | var lovers = 0 20 | var name = "鸳鸯" 21 | 22 | //可以更改父类中的方法 23 | override func growUp(color: String) { 24 | //调用父类的方法 25 | super.growUp(color: color) 26 | self.lovers = 1 27 | print("鸳鸯长大了!有了\(self.lovers)个伴侣!") 28 | } 29 | 30 | //计算属性:由其他属性间接得出(set),也可以影响其他属性(set)。 31 | var desc : String { 32 | get { 33 | return "\(self.name)是一种中国特有的鸭子种类,一生只有钟情于一个伴侣。" 34 | } 35 | } 36 | } 37 | 38 | let duck1 = MandarinDuck(color: "gray", foot: 2) 39 | duck1.growUp(color: "Rainbow") 40 | print(duck1.desc) 41 | 42 | 43 | //: [>](@next) 44 | -------------------------------------------------------------------------------- /8.面向对象.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /8.面向对象.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /8.面向对象.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/8.面向对象.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /8.面向对象.playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | BuildLocationStyle 6 | UseAppPreferences 7 | CustomBuildLocationType 8 | RelativeToDerivedData 9 | DerivedDataLocationStyle 10 | Default 11 | IssueFilterStyle 12 | ShowActiveSchemeOnly 13 | LiveSourceIssuesEnabled 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /8.面向对象.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/8.面向对象.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /9.属性详解(选修).playground/Pages/实例属性.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ---- 2 | //: - [小波说雨燕 3天学会Swift 4](http://www.xiaoboswift.com) 3 | //: ---- 4 | //: - 属性: 对象的特征,定义为变量。 5 | //: - 实例属性: 实例化后才能使用 6 | //: ---- 7 | //: - 存储属性: 可以直接赋值的变量 8 | struct Role { 9 | var hero = "" 10 | var cash = 0 11 | } 12 | var role1 = Role(hero: "曹操", cash: 100) 13 | 14 | //: - 计算属性: 由其他属性间接得出 15 | //: - get { //组合方法 } 16 | //: - set { //有新值时如何影响其他属性 } 17 | 18 | //: - 🌰玩家的当前坐标,与移动方式相关 19 | 20 | struct Point { 21 | var x = 0, y = 0 22 | } 23 | 24 | enum Move { 25 | case walk 26 | case teleport(Point) 27 | } 28 | 29 | class Player { 30 | var point = Point() 31 | var move = Move.walk 32 | 33 | var currentPoint: Point { 34 | get { 35 | switch move { 36 | case .walk: 37 | point.x += 1 38 | point.y += 1 39 | case .teleport(let customPoint): 40 | point.x += customPoint.x 41 | point.y += customPoint.y 42 | } 43 | return point 44 | } 45 | set { 46 | point = newValue 47 | } 48 | } 49 | } 50 | 51 | var player1 = Player() 52 | player1.move = .walk 53 | player1.currentPoint 54 | 55 | let myPoint = Point(x: 100, y: 100) 56 | player1.move = .teleport(myPoint) 57 | player1.currentPoint 58 | 59 | 60 | //计算属性如何影响其他属性 61 | player1.currentPoint = Point(x: 200, y: 200) 62 | player1.point 63 | 64 | //: - 👀属性监视器: 对属性变化的响应机制 65 | //: - willSet: 事前响应(即将设置的新值: newValue) 66 | //: - didSet : 事后响应(已被更新的旧值: oldValue) 67 | struct Exp { 68 | var total = 0 { 69 | willSet { 70 | print("当前总经验:\(newValue)") 71 | } 72 | didSet { 73 | print("增加经验点:\(total - oldValue)") 74 | } 75 | } 76 | } 77 | 78 | var exp1 = Exp() 79 | exp1.total = 1000 80 | 81 | 82 | //: [>](@next) 83 | -------------------------------------------------------------------------------- /9.属性详解(选修).playground/Pages/实例属性.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | 29 | 30 | 34 | 35 | 39 | 40 | 44 | 45 | 49 | 50 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /9.属性详解(选修).playground/Pages/类型属性(选修).xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 类型属性 :属于类型固有的,实例不能调用. 2 | //: - ⚠️对于类, 计算型的类型属性,前面加关键字class可被子类重写(override) 3 | 4 | //单细胞生物 5 | class Unicelluar { 6 | //遗传方式 7 | class var inherit :String { 8 | return "RNA" 9 | } 10 | 11 | } 12 | 13 | Unicelluar.inherit 14 | 15 | //人类也继承了单细胞生物的特性 16 | class Human: Unicelluar { 17 | override class var inherit :String { 18 | return "DNA" 19 | } 20 | } 21 | 22 | Human.inherit 23 | 24 | 25 | 26 | //: - 💡结构体和枚举, 关键字 static 27 | struct Shopping { 28 | static let brand = "UNIClO" 29 | static let net = "http://www.taobao.com?cate=" 30 | var cate = "" 31 | 32 | var url : String { 33 | return Shopping.net + cate 34 | } 35 | } 36 | 37 | Shopping.brand 38 | 39 | let shopping1 = Shopping(cate: "seafood") 40 | 41 | shopping1.url 42 | 43 | 44 | //: [](@next) 45 | -------------------------------------------------------------------------------- /9.属性详解(选修).playground/Pages/类型属性(选修).xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /9.属性详解(选修).playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /9.属性详解(选修).playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /9.属性详解(选修).playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/9.属性详解(选修).playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /9.属性详解(选修).playground/playground.xcworkspace/xcuserdata/xiaobo.xcuserdatad/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | BuildLocationStyle 6 | UseAppPreferences 7 | CustomBuildLocationType 8 | RelativeToDerivedData 9 | DerivedDataLocationStyle 10 | Default 11 | IssueFilterStyle 12 | ShowActiveSchemeOnly 13 | LiveSourceIssuesEnabled 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /9.属性详解(选修).playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/9.属性详解(选修).playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /JSON.playground.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/JSON.playground.zip -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # swift2basic 2 | 3天学会Swift 4(苹果官网Swift教程精华版 & JSON解析) 3 | 4 | 小波说雨燕:www.xiaoboswift.com 5 | 6 | 支持群:109816704 8477435 18247468 7 | 8 | ✨本课程已被选为苏州园区中学生指定编程入门课之一 9 | -------------------------------------------------------------------------------- /大作业参考:房贷计算器.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | //房屋总价 4 | let price = 1_500_000.0 5 | //贷款利率 6 | let rate = 4.9 / 100 7 | //分期年数 8 | let years = 30 9 | //首付利率 10 | let downPayRate = 0.3 11 | 12 | //本金 13 | let principal = (price * (1 - downPayRate)) / Double(years * 12) 14 | //贷款(逐月递减) 15 | var loan = price * (1 - downPayRate) 16 | 17 | //分期月数列表 18 | //let months = Array(1...(years * 12)) 19 | 20 | //每个月利息列表 21 | //每个月要还的利息费用,是当月还没有未还款的贷款 * 月利息,所以利息是递减的 22 | //let monthlyInterest = months.map {_ -> Double in 23 | // let currentMonthInterest = loan * (rate / 12) 24 | // loan -= principal 25 | // return currentMonthInterest 26 | //} 27 | 28 | var interests : [Double] = [] 29 | for _ in 1...(years * 12) { 30 | let currentMonthInterest = loan * (rate / 12) 31 | loan -= principal 32 | interests.append(currentMonthInterest) 33 | } 34 | 35 | //每月利息列表 36 | let desc = interests.reduce("每月利息:") { "\($0) \($1)"} 37 | 38 | //总利息 - 数字型数组的计数 reduce, sum 39 | let totalInterests = interests.reduce(0.0) { $0 + $1} 40 | totalInterests 41 | 42 | //总花费 43 | print("总花费:", Int(price + totalInterests)) 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /大作业参考:房贷计算器.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /大作业参考:房贷计算器.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /大作业参考:房贷计算器.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/大作业参考:房贷计算器.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /大作业参考:房贷计算器.playground/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 11 | 15 | 16 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /第1天大作业.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | //: 第1天大作业 2 | 3 | //: 用面向过程的技能,实现一个房贷计算器。每个同学,必须阐述实现原理。 4 | 5 | 6 | //: 假设小波贷款买了一套房,房贷总价 150万元,利率4.9%,分期30年,首付3成。 求按等额本金还款方式下,每个月利息列表 和 购买此套房的总花费。 7 | 8 | -------------------------------------------------------------------------------- /第1天大作业.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /第1天大作业.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /第1天大作业.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagamis/swift2basic/c8083be8ca2b348c8cd1c693655647ca274f50d7/第1天大作业.playground/playground.xcworkspace/xcuserdata/yons.xcuserdatad/UserInterfaceState.xcuserstate --------------------------------------------------------------------------------