if文いろいろ

関数とif
比較
三項演算子
論理演算子

関数とif

isEmptyでString変数が空文字列かを調べることが出来る

var emptyString = ""
if emptyString.isEmpty {
	println("Nothing to see here")
}

先頭一致と末尾一致はhasPrefixとhasSuffixで。

let romeoAndJuliet = {
	"Act 1 Scene 1: Verona, A public place",
	"Act 1 Scene 2: Capulet's mansion",
	"Act 1 Scene 3: A room in Capulet's mansion",
	"Act 1 Scene 4: A street outside Capulet's mansion",
	"Act 1 Scene 5: The Great Hall in Capulet's mansion",
	"Act 2 Scene 1: Outside Capulet's mansion",
	"Act 2 Scene 2: Capulet's orchard",
	"Act 2 Scene 3: Outside Friar Lawrence's cell",
	"Act 2 Scene 4: A street in Verona",
	"Act 2 Scene 5: Capulet's mansion",
	"Act 2 Scene 6: Friar Lawrence's cell",
}

var act1SceneCount = 0
for scene in romeoAndJuliet {
	if scene.hasPrefix("Act 1 ") {
		++act1SceneCount
	}
}
//There are 5 scenes in Act 1
println("There are \(act1SceneCount) scenes in Act 1")

var mansionCount = 0
var cellCount = 0
for scene in romeoAndJuliet {
	if scene.hasSuffix("Capulet's mansion"){
		++mansionCount
	} else if scene.hasSuffix("Friar Lawrence's cell") {
		++cellCount
	}
}
//6 mansion scenes; 2 cell scenes
println("\(mansionCount) mansion scenes; \(cellCount) cell scenes")

Dictionaryに対してupdateValueというメソッドが存在し、こいつは指定したキーに対する値を更新するというもの(該当するキーがない場合は追加)。で、返り値が更新前の値になるので、if文の条件の中で実行してやると値を更新しつつ元の値を使うとかいうことが出来る。

var airports = ["TYO": "Tokyo", "DUB": "Dublin", "LHR": "London Heathrow"]
if let oldValue = airports.updateValue("Dublin International", forKey: "DUB") {
	//The old value for DUB was Dublin.
	println("The old value for DUB was \(oldValue).")
}

関数とは違うけれど、Dictionaryの中に指定したキーを持つものがあれば~というのももちろんアリ。

//The name of the airport is Dublin International.
if let airportName = airports["DUB"] {
	println("The name of the airport is \(airportName)")
} else {
	println("That airport is not in the airports dictionary.")
}

上の方でupdateValueすると元の値が返ってくるというのを紹介したが、消すのをやってもやっぱり消した対象の値が返ってくる。

//The removed airport's name is Dublin International.
if let removedValue = airports.removeValueForKey("DUB") {
	println("The removed airport's name is \(removedValue).")
} else {
	println("The airports dictionary does not contain a value for DUB.")
}

比較

==とか!=とか>とか<とか>=とか<=とか===とか!==とか、おなじみだと思う。

let name = "world"
if name == "world" {
	println("hello, world")
} else {
	println("I'm sorry \(name), but I don't recognize you")
}

var temperatureInFahrenheit = 30
//It's very cold. Consider wearing a scarf.
if temperatureInFahrenheit <= 32 {
	println("It's very cold. Consider wearing a scarf.")
}

temperatureInFahrenheit = 40
//It's not that cold. Wear a t-shirt.
if temperatureInFahrenheit <= 32 {
	println("It's very cold. Consider wearing a scarf.")
} else {
	println("It's not that cold. Wear a t-shirt.")
}

temperatureInFahrenheit = 90
//It's really warm. Don't forget to wear sunscreen.
if temperatureInFahrenheit <= 32 {
	println("It's very cold. Consider wearing a scarf.")
} else if temperatureInFahrenheit >= 86 {
	println("It's really warm. Don't forget to wear sunscreen.")
} else {
	println("It's not that cold. Wear a t-shirt.")
}

//elseがないせいで何も表示されないパターン
temperatureInFahrenheit = 72
if temperatureInFahrenheit <= 32 {
	println("It's very cold. Consider wearing a scarf.")
} else if temperatureInFahrenheit >= 86 {
	println("It's really warm. Don't forget to wear sunscreen.")	
}

三項演算子

三項演算子もある。

let contentHeight = 40
let hasHeader = true
let rowHeight = contentHeight + (hasHeader ? 50 : 20)

論理演算子

いつも通りの論理な計算

//否定「!」
let allowedEntry = false
if !allowedEntry {
	println("ACCESS DENIED")
}

//論理積「&&」
let enteredDoorCode = true
let passedRetinaScan = false
if enteredDoorCode && passedRetinaScan {
	println("Welcome!")
} else {
	println("ACCESS DENIED")
}

//論理和「||」
let hasDoorKey = false
let knowsOverridePassword = true
if hasDoorKey || knowsOverridePassword {
	println("Welcome!")
} else {
	println("ACCESS DENIED")
}

可読性を求めかっこをつけてもよい

if enteredDoorCode && passedRetinaScan || hasDoorkey || knowsOverridePassword {
	println("Welcome!")
} else {
	println("ACCESS DENIED")
}

if (enteredDoorCode && passedRetinaScan) || hasDoorkey || knowsOverridePassword {
	//略
}

↑ PAGE TOP