8. guard let
• 二つの引数が nil でない事を保証する
func add(a: Int?, _ b: Int?) -> Int? {
guard let a = a, let b = b else { return nil }
return a + b
}
9. if let
• if let を使う
func add(a: Int?, _ b: Int?) -> Int? {
if let a = a, let b = b {
return a + b
}
return nil
}
10. where
• guard let で0以上を保証する
func add(a: Int?, _ b: Int?) -> Int? {
guard let a = a, let b = b where a >= 0 && b >= 0 else {
return nil
}
return a + b
}
• if let でも0以上を保証する
func add(a: Int?, _ b: Int?) -> Int? {
if let a = a, let b = b where a >= 0 && b >= 0 {
return a + b
}
return nil
}
13. Optional in Switch
• 実は optional も switch 文に使える
func countryName(identifier: String?) -> String? {
switch identifier {
case "JP"?: return "Japan"
case "GB"?: return "England"
case "US"?: return "U.S.A."
default: return nil
}
}
14. Late Initializing
Immutable Variables
• 初期値を後で設定する不定変数
let color: UIColor // "= value"
switch value % 3 {
case 0: color = UIColor.whiteColor()
case 1: color = UIColor.redColor()
default: fatalError()
}
• 初期化しないケースがあると、コンパイルエラー
15. Complex switch cases
for thing in things {
switch thing {
case 0 as Int:
print("zero as an Int")
case 0 as Double:
print("zero as a Double")
case let someInt as Int:
print("an integer value of (someInt)")
case let someDouble as Double where someDouble > 0:
print("a positive double value of (someDouble)")
case is Double:
print("some other double value")
case let someString as String:
print("a string value of "(someString)"")
case let (x, y) as (Double, Double):
print("an (x, y) point at (x), (y)")
case let movie as Movie:
print("Movie:'(movie.name)'")
default:
print("something else")
}
}
20. Defining Associated
Value
enum Element {
case String(Swift.String)
case Boolean(Bool)
case Integer(Int)
case Float(Swift.Float)
case Dictionary([Swift.String: Element])
case Array([Element])
case Null
}
let integer = Element.Integer(42)
let city = Element.String("Tokyo")
let cities = Element.Array([city])
let dictionary = Element.Dictionary(["items": array])
21. Extracting Associated Values
Using switch Statement
• Associated Value を Switch文で取り出す
switch element {
case .String(let string): print("string: (string)")
case .Boolean(let value): print("boolean: (value)")
case .Integer(let value): print("ineteger: (value)")
case .Float(let value): print("float: (value)")
case .Dictionary(let dictionary):
print("dictionary: (dictionary)")
case .Array(let array): print("array: (array)")
case .Null: print("null")
}
22. Extracting Associated
Values using if Statement
• If 文でも取り出せます
let element1: Element = …
if case .String(let string) = element1 {
print("(string)")
}
• Optional な場合でも取り出せます
let element: Element? = …
if case .String(let string)? = element1 {
print("(string)")
}
23. Extracting Associated
Values
• division -> members -> person -> name
let name = Element.String("John")
let john = Element.Dictionary(["name": name])
let members = Element.Array([john])
let group = Element.Dictionary(["members": members])
• 一発で取り出せる
if case .Dictionary(let group) = group,
case .Array(let members)? = division["members"],
case .Dictionary(let member)? = members.first,
case .String(let name)? = member["name"] {
print("(name)") // John
}
39. Singleton
• 典型的なシングルトン
class Manager {
static let sharedManager = Manager()
private init() {
}
}
• クロージャーを使ったシングルトン
class Manager {
static var sharedManager: Manager = {
return Manager()
}()
private init() {
}
}
http://qiita.com/codelynx/items/a936afe0a45d4cf5abfb
40. Updating C style
`for` statement
http://qiita.com/codelynx/items/899c26dd2cbdba7d2b00
41. for var i = 0 ; i < 100 ; i++
// C-Style for statement
for var i = 0 ; i < 100 ; i++ {
print("(i)")
}
// Swift 3.0 ready
(0 ..< 100).forEach { print("($0)") }
// Swift 3.0 ready
for i in (0 ..< 100) {
print("(i)")
}
42. for var i = 99 ; i >= 0 ; i--
// C-Style for statement
for var i = 99 ; i >= 0 ; i-- {
print("(i)")
}
// Swift 3.0 ready
(0 ..< 100).reverse().forEach { print("($0)") }
// Swift 3.0 ready
for i in (0 ..< 100).reverse() {
print("(i)")
}
43. for var i = 0; i < 100 ; i += 2
// C-Style for statement
for var i = 0; i < 100 ; i += 2 {
print("(i)")
}
// Swift 3.0 ready
0.stride(to: 100, by: 2).forEach { print("($0)") }
44. for var i = 98 ; i >= 0 ; i -=
2
// C-Style for statement
for var i = 98 ; i >= 0 ; i -= 2 {
print("(i)")
}
// Swift 3.0 ready
98.stride(through: 0, by: -2).forEach { print("($0)") }
// Swift 3.0 ready
0.stride(to: 100, by: 2).reverse().forEach { print("($0)") }
// Swift 3.0 ready
for i in 0.stride(to: 100, by: 2).reverse() {
print("(i)")
}
45. for without increment
• 次の再初期化式の指定がなく、刻みが不定の場合
// C-Style for statement
for var i = 0 ; i < 100 ; {
print("(i)")
if (i * i) % 2 == 0 { i += 1 }
else { i += 2 }
}
// Swift 3.0 ready
var i = 0
while i < 100 {
print("(i)")
if (i * i) % 2 == 0 { i += 1 }
else { i += 2 }
}