admin管理员组文章数量:1621899
//: Playground - noun: a place where people can play
import UIKit
//repeat {
//
//} while aValue == bValue
// 课堂练习一: 使用repeat while
var aWin = false
var bWin = false
repeat {
let aRandomValue = arc4random_uniform(6) + 1
let bRandomValue = arc4random_uniform(6) + 1
if aRandomValue > bRandomValue {
aWin = true
} else if aRandomValue < bRandomValue {
bWin = true
} else {
print("Draw!")
}
} while !aWin && !bWin
// 验证谁赢
let finalWinner = aWin ? "A" : "B"
print("\(finalWinner) wins in the first game!")
// 课堂练习二:使用while死循环
while true {
let aRandomValue = arc4random_uniform(6) + 1
let bRandomValue = arc4random_uniform(6) + 1
if aRandomValue == bRandomValue {
print("Draw!")
continue
}
let winner = aRandomValue > bRandomValue ? "A" : "B"
print("\(winner) wins in second game!")
break
}
// 课堂练习三:
let someValue = 10
// 方式一: 使用区间运算符
// Int.min获取Int类型的最小值
Int.min
switch someValue {
case Int.min..<0:
print("The value is negative!")
case 0:
print("The value is zero!")
default:
print("The value is positive!")
}
// 方式二: 使用where模式匹配
switch someValue {
case let x where x < 0:
print("The value is negative!")
case let x where x == 0:
print("The value is zero!")
default:
print("The value is positive")
}
Day02
回顾:
1. 常量和变量
const int imInt = 10; // 编译时已经知道整型
let imInt = 10 // 运行时才知道整型
var imInt = 10 // 多次初始化
2. 基本数据类型: Int / Float / Double / String / Character / Bool —> 都是结构体(属性和方法)
3. 元组: 使用逗号分隔, 使用小括号包含多个相同或者不同的数据的数据结构
3.1 获取元组分量: .0 .1 .2 …..
3.2 给元组分量命名, 通过名字的方式获取分量的值
4. 区间运算符: 和switch和for循环结合紧密
a. 闭区间 x…y
b. 开区间 x..<y
今天内容:
· 可选型Optional(***重点和难点)
· 控制流语句: if / else / for / while / repeat while
· 容器类: 数组和字典(**)
可选型Optional
1. 背景/为什么:
C语言的main函数返回int, 如果返回0, 表明程序没有bug; 返回负值, 表明程序有错误.
—> 需求: 给定字符串, 返回该字符串的整数值
定义函数: int convertIntValue(char *) { // 代码逻辑 }
a. “123” -> 123
b. “hello” -> 返回什么值, 表示无法转成整型-934285623846
2. 是什么?
描述: 新增的类型Optional, 用来控制值的存在性, 如果存在, 赋值某个类型的值; 如果不存在, 给nil
—> Swift, 不可以个任意类型赋值为nil, 除了可选型Optional
—> 无法单独存在, 和其他的类型结合使用
a. Int? 称为整型可选型; String? 字符串可选型.....
b. var imInt: Int = 10
imInt = nil // 有编译错误
c. Swift语言:
func convertIntValue(inputStr: String) -> Int? {
// 如果转不成功, 返回nil
// 如果能转成功, 返回整型的值
}
3. 样例: 可选型的声明/使用/解包
[ 01_optional ]
/05_Swift/Day02/Day02-AM1.zip
4. 样例: 可选型的其他用法
[ 02_other_optional ]
—> 可选型总结:
a. 和其他类型结合使用: Int? String? Bool? …..
b. 存在: 对应类型有值; 不存在: nil
c. 解包: 强制解包!; if let解包; nil聚合 ?? ; 链表达?.
5. 样例: 控制语句control flow语句: if / else / switch / for / while / repeat while
[ 03_control_flow ]
/05_Swift/Day02/Day02-AM2.zip
———————— 下午内容 ————
1. 样例: switch语句的使用
[ 04_switch_statement ]
2. 样例: 数组(结构体)的使用
[ 05_array ]
OC:
NSMutableArray *mutableArray = [NSMutableArray new];
[mutableArray addObject: @10];
[mutableArray addObject: @“hello”];
NSArray *newArray = mutableArray;
[mutableArray addObject: @1000];
C:
int intOne = 10;
int intTwo = intOne;
intOne = 20;
/05_Swift/Day02/Day02-PM1.zip
3. 课堂练习一和二:
[ 06_practice ]
3.1 需求: 两个人投色子, 两个人投掷的一样, 循环继续; 只要一个人投掷点数大于另一个人, 循环结束
3.2 要求一:
repeat while(仿照C语言的do while循环, 把do关键词改成repeat)实现上述需求
3.3 要求二:
while死循环实现上述需求(提示: break/continue)
4. 课堂练习三:
4.1 将下面的if/else if语句改成switch语句

5. 复习可选型Optional: 将上午的可选型代码重新写一遍, 或者将官方文档的可选型代码重新写一遍.
/05_Swift/Day02/Day02-PM2.zip
Day02重要知识点总结:
1. 掌握可选型的基本语法; 了解可选型存在含义
2. 掌握可选型的几种解包方式
a. 强制解包(风险)
b. if let解包(可选型绑定Optional Binding)
c. nil聚合(nil-coalescing)语法
d. 可选型链式表达(Optional Chaining)
3. 掌握数组的创建和初始化
4. 理解”值类型”和”引用类型”概念([05_array.playground])
版权声明:本文标题:Swift 系统学习 09 repeat while 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1728852269a1176819.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论