制御構文
条件分岐、繰り返し、遅延実行、パターンマッチ
プログラムの実行フローを制御する構文。制御フローの制御には条件分岐や繰り返しなどがあり、それを組み合わせ実行し自在に操る
for-in文 シーケンスの要素の列挙
chapter5.swift
let array = [1,2,3]
for element in array {
print(element)
}
Dictionary
chapter5.swift
let dictionary = ["a": 1, "b": 2]
for (key,value) in dictionary {
print("Key: \(key), vlue: \(value)")
}
while文 継続条件による繰り返
chapter5.swift
var f = 1
while f < 4 {
print(f)
f += 1
}
repeat while文
while文は実行前に条件式を評価するため、場合によっては一度も実行されない
そこで条件式の成否にかかわらず、必ず1回以上繰り返しを実行したい場合は、repear whieを使用
chapter5.swift
var t = 1
repeat {
print(t)
t += 3
}
while t < 1
実行中の中断
break文は繰り返し文全体を終了させます。
chapter5.swift
var containsTwo = false
let garray = [1,2,3]
for element in garray {
if element == 2 {
containsTwo = true
break
}
print("element:\(element)")
}
print("containsTwo:\(containsTwo)")
continue文は現在の処理のみを中断し、後続の処理を継続する
chapter5.swift
var odds = [Int]()
let rarray = [1,2,3,4,5,6]
for element in rarray {
if element % 2 == 1 {
odds.append(element)
continue
}
print("even:\(element)")
}
print("odds:\(odds)")
}
遅延実行 のためのdefer文
defer文 スコープ退出時の処理
chapter5.swift
var count = 0
func someFUNTIONS() -> Int{
defer {
count += 1
}
return count
}
print(someFUNTIONS())
print(someFUNTIONS())
print(count)
パターンマッチ 値の構造や性質による評価
swiftには値の持つ構造や性質を表現するパターンという概念がある
パターン待ちとは、値が特定のパターンに合致するかの検査をすること
式パターン ~ = 演算子による評価
chapter5.swift
let integer = 6
switch integer {
case 6:
print("match: 6")
case 5...10:
print("match: 5...10")
default:
print("default")
}
バリューバインディング 値の代入を伴う評価
chapter5.swift
let values = 3
switch values {
case let matchValue :
print(matchValue)
}
オプショナルパターン Optional型の値の有無の評価
chapter5.swift
let values = 3
let optionalD = Optional(4)
switch optionalD{
case let f?:
print(f)
default:
print("nil")
}
}
列挙型ケースパターン ケースとの一致の評価
chapter5.swift
enum Hemisphere {
case northern
case southern
}
let hemisphere = Hemisphere.southern
//let hemisphere = Optional(Hemisphere.southern)
switch hemisphere {
case .northern:
print("match: .northern")
case .southern:
print("match: .southern")
//case nil:
// print("nil")
}
enum Color {
case rgb(Int,Int,Int)
case cmyk(Int,Int,Int,Int)
}
let color = Color.rgb(100, 200, 230)
switch color {
case .rgb(let r, let h, let j):
print(".rgb: (\(r), \(h), \(j))")
case .cmyk(let k,let y, let u, let o):
print(" .cmyk: (\(k),\(y),\(u),\(o))")
}
is演算子による型キャスティングパターン 型の判定による評価
chapter5.swift
let any: Any = "fafafa"
switch any {
case is String:
print("match: String")
case is Int:
print("match: Int")
default:
print("default")
}
}
as演算子による型キャスティングパターン 型のキャストによる評価
chapter5.swift
let anys : Any = "1"
switch anys {
case let string as String:
print("match: string(\(string))")
case let int as Int:
print("match: int(\(int))")
default:
print("default")
}
パターンマッチが使える場所if 文
chapter5.swift
let fff = 9
if case 1...10 = fff {
print("yes")
}
//guard 文
func someFunc() {
let value = 9
guard case 1...10 = value else {
return
}
print("yes")
}
someFunc()
//for in文
let harray = [1,2,3,4,5,6]
for case 2...5 in harray {
print("2以上5以下")
}
while 文
chapter5.swift
var nextValue = Optional(1)
while case let value? = nextValue {
print("value:\(value)")
if value >= 8 {
nextValue = nil
} else {
nextValue = value + 1
}
}