携手创作,共同生长!这是我参与「日新方案 8 月更文应战」的第19天,点击检查活动详情
最近在学习移动端开发,记录一下曾经端视角学习 IOS 开发的过程。这是我整理了 Swift4 的学习笔记。
闭包 Closure
闭包的根本运用与简写
- 语法优化
var myClosure:() -> Void = {
print("Hello world")
}
myClosure()
var mySecondClosure:(Int,Int) -> Int = {
(a:Int,b:Int) -> Int in //闭包规范写法
return a*b
}
mySecondClosure = {
(a,b) in //闭包简写,类型主动推导 能够根据参数推断
return a*b
}
mySecondClosure = {
(a,b) in //进一步闭包简写,如果函数体只包括一句 return 代码,可省掉 return
a*b
}
mySecondClosure = {
$0 * $1
//再简写:被捕获的参数列表中,含有a、b,下标从0开端,可通过"$"获取。省掉参数列表 (a, b)和 关键字 in
}
闭包应用
- 排序
var arr:[Int] = [1,3,5,6,7,2,4,6,8]
arr.sorted() //[1, 2, 3, 4, 5, 6, 6, 7, 8]
arr.sorted {
$0 > $1 //[8, 7, 6, 6, 5, 4, 3, 2, 1]
}
var arr = [0,1,2,3,4,5,6,7,8,9,10]
arr.sorted {
return fabs(Float($0-5)) < fabs(Float($1-5)) //离5越近排在越前面 [5, 4, 6, 3, 7, 2, 8, 1, 9, 0, 10]
}
//forEach
var arr2 = [5,6,7,8,9]
arr2.forEach {
print("\($0)") //[5, 6, 7, 8, 9]
}
var arr3 = [1,2,3,4]
//filter
arr3.filter {
return $0 > 3 //[4]
}
var arr4 = ["0","1","imagine"]
//map
var num = arr4.map {
Int($0) //[0, 1, nil] 类型转化可能失利,所以返回的是可选型
}
var num2 = arr4.compactMap {
Int($0) //[0, 1] //compactMap : 解包->展开并合并
}
//reduce
var arr5 = [0,1,2]
var sum = arr5.reduce(0) {
return $0 + $1 //3
}
闭包作为函数参数
func handler(_ a: Int, _ b: Int,operation:(Int,Int)->Int) ->Int {
let res = operation(a,b)
return res
}
let multipyClosure = { //实现一个闭包
(a:Int,b:Int) in
a * b
}
handler(2, 3, operation: multipyClosure) //将闭包作为参数传递
捕获
//捕获
var num = 3
arr.sorted {
return fabs(Float($0-num)) < fabs(Float($1-num)) //向外层找变量 num
}
枚举根本运用
表述一组值
枚举相当于创建了一种新的数据类型,而类型的取值由里面的case值进行表征
enum CompassPoint { // 大写最初
case north,west,east,south
}
enum GameEnding {
case Win
case Lose
case Draw
}
var yourScore:Int = 100
var enemyScore:Int = 100
var thisGameEnding:GameEnding
if yourScore > enemyScore {thisGameEnding = GameEnding.Win}
else if yourScore == enemyScore {thisGameEnding = GameEnding.Draw}
else {thisGameEnding = .Lose} //可省掉GameEnding
switch thisGameEnding
{
case .Win: print("win")
case .Draw: print("Draw")
case .Lose: print("Lose")
}
enum VowleCharacter:Character {
case A = "a"
case E = "e"
case I = "i"
case O = "o"
case U = "u"
}
let vowelA = VowleCharacter.A
var userInputCharacter:Character = "a"
if userInputCharacter == vowelA.rawValue
{
print("it is an 'a'") //"it is an 'a'\n"
}else {
print("it is not an 'a'")
}
灵活运用
enum Barcode {
case UPCA(Int,Int,Int,Int)
case QRCode(String) //将枚举变量QRCode相关为String类型
}
let productCodeA = Barcode.UPCA(4, 102, 306, 8)
let productCodeB = Barcode.QRCode("This is a infomation")
switch productCodeA {
case .UPCA(let numberSystem,let manufacture,let identifier,let check):
print("UPC-A with value of \(numberSystem), \(manufacture), \(identifier),\(check).") //"UPC-A with value of 4, 102, 306,8.\n"
case .QRCode(let productCode):
print("QRCode with value of \(productCode).")
}