swift 3.0 の新しい機能(のうちの9つ)
TRANSCRIPT
![Page 1: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/1.jpg)
![Page 2: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/2.jpg)
![Page 3: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/3.jpg)
紙版は絶版、電⼦書籍は販売中
![Page 4: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/4.jpg)
![Page 6: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/6.jpg)
![Page 7: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/7.jpg)
![Page 8: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/8.jpg)
![Page 9: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/9.jpg)
![Page 10: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/10.jpg)
![Page 12: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/12.jpg)
![Page 13: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/13.jpg)
![Page 14: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/14.jpg)
![Page 15: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/15.jpg)
![Page 16: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/16.jpg)
![Page 17: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/17.jpg)
![Page 18: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/18.jpg)
:
@available(*, unavailable, renamed: "NewProtocol") typealias PreviousProtocol = NewProtocol
![Page 19: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/19.jpg)
![Page 20: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/20.jpg)
![Page 21: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/21.jpg)
renamed message
@swift3_migration(message: "MESSAGE") func doSomething() { … }
@swift3_migration(renamed: "NewProtocol") typealias PreviousProtocol = NewProtocol
![Page 22: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/22.jpg)
![Page 23: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/23.jpg)
![Page 24: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/24.jpg)
if /* comment */!isValid { ... }
value +/* comment */coefficient
!
+/*
![Page 25: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/25.jpg)
optionalArray/* Comment */?.count
value+/* comment */coefficient
?
+
![Page 26: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/26.jpg)
![Page 27: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/27.jpg)
![Page 28: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/28.jpg)
!
let optionalValue: Int! = 100
![Page 29: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/29.jpg)
var a: Int! var b: Int?
// なぜか ImplicitlyUnwrappedOptional<Int> として認識 print(a.dynamicType)
// こちらは Optional<Int> 型として認識 print(b.dynamicType)
![Page 30: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/30.jpg)
![Page 31: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/31.jpg)
![Page 32: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/32.jpg)
func function(first: Int, second: Int, third: Int) {
}
function(first: 1, second: 10, third: 3)
![Page 33: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/33.jpg)
_
// ラベル名を label にする func function(label value: Int) {
}
func function(_ label value: Int) {
}
![Page 34: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/34.jpg)
![Page 35: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/35.jpg)
![Page 36: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/36.jpg)
func function(@noescape f: () -> Int) {
}
func function(@autoclosure f: () -> Int) {
}
![Page 37: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/37.jpg)
func function(f: @noescape () -> Int) {
}
func function(f: @autoclosure () -> Int) {
}
![Page 38: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/38.jpg)
![Page 39: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/39.jpg)
func function(f: () -> Int) -> (@noescape (Int) -> Int) -> Int {
return { g in g(f()) }
}
![Page 40: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/40.jpg)
func method() -> Int { let g: @noescape (Int) -> Int = { $0 + action() * 2 } return function(g) }
![Page 41: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/41.jpg)
func method() -> Int { func g(_ value: Int) -> Int { return value + action() * 2 } return function(g) }
![Page 42: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/42.jpg)
func function(@noescape f: () -> Int) -> (@noescape (Int) -> Int) -> Int {
return { g in g(f()) }
}
![Page 43: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/43.jpg)
func function(@noescape f: () -> Int) -> @noescape (@noescape (Int) -> Int) -> Int {
return { g in g(f()) }
}
![Page 44: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/44.jpg)
![Page 45: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/45.jpg)
func function() -> (@autoclosure () -> Int) -> Int {
return { (f: @autoclosure () -> Int) in f() } }
// 関数で得た、関数を取る関数に、値をそのまま渡せる let f = function() let result = f(100)
![Page 46: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/46.jpg)
let f: () -> (@autoclosure () -> Int) -> Int = {
return { (f: @autoclosure () -> Int) in f() } }
// 関数を取る関数に、値をそのまま渡せる let result = f(100)
![Page 47: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/47.jpg)
let value: @autoclosure () -> Int = {
return 100 }
![Page 48: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/48.jpg)
![Page 49: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/49.jpg)
![Page 50: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/50.jpg)
func method(inout result: Int) {
}
func method(inOut result: Int) {
}
![Page 51: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/51.jpg)
func method(result: inout Int) {
}
func method(inOut result: inout Int) {
}
![Page 52: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/52.jpg)
func method(inout result: inout Int) {
}
func method(`inout` result: inout Int) {
}
![Page 53: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/53.jpg)
func method(result: inout Int) {
}
func method(inOut result: inout Int) {
}
![Page 54: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/54.jpg)
func function() -> (result: inout Int) -> Void {
return { (result: inout Int) -> Void in
result = 700 } }
![Page 55: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/55.jpg)
func function() -> (result: inout Int) -> Void {
return { $0 = 700 }
}
![Page 56: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/56.jpg)
let f: (inout Int) -> Void = {
return { $0 = 5000 }
}
![Page 57: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/57.jpg)
![Page 58: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/58.jpg)
![Page 59: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/59.jpg)
![Page 60: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/60.jpg)
func freeFall(context: Context)(time: Double) -> Double { return -1 / 2 * time * time * context.gravitationalAcceleration }
![Page 61: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/61.jpg)
func freeFall(context: Context) -> (time: Double) -> Double {
return { time in
-1 / 2 * time * time * context.gravitationalAcceleration
} }
![Page 62: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/62.jpg)
// コンテキストを固定し、その環境を前提に動く関数を定義 let earth = Context(identifier: "Earth", gravitationalAcceleration: 9.80665)
let freeFallOnEarth: (time: Double) -> Double = freeFall(context: earth)
// 汎用関数でスマートに捌く let timeInterval = stride(from: 0.0, through: 100.0, by: 1.0)
let locations = timeInterval.map(freeFallOnEarth)
![Page 63: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/63.jpg)
func function(object: Object)(multiple: Int) -> Int {
let value = object.value return value * multiple }
do { let f = function(Object(100)) print(f(multiple: 5)) }
![Page 64: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/64.jpg)
func function(object:Object) -> (multiple:Int) -> Int {
let value = object.value return { multiple in value * multiple } }
do { let f = function(Object(100)) print(f(multiple: 5)) }
![Page 65: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/65.jpg)
![Page 66: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/66.jpg)
![Page 67: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/67.jpg)
func function(var initial value: Int, count: Int) -> Int {
(1 ..< count).forEach { _ in
value += value }
return value }
![Page 68: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/68.jpg)
func function(var initial value: Int, count: Int) -> Int {
(1 ..< count).forEach { _ in
value += value }
return value }
var value = 10 let answer = function(initial: value, count: 5)
![Page 69: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/69.jpg)
func function(initial value: inout Int, count: Int) {
(1 ..< count).forEach { _ in
value += value }
}
var value = 10
function(initial: &value, count: 5)
![Page 70: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/70.jpg)
func function(var initial value: Int, count: Int) -> Int {
// 即座に value に変更を仕掛けていける (1 ..< count).forEach { _ in
value += value }
return value }
![Page 71: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/71.jpg)
func function(initial value: Int, count: Int) -> Int {
// 編集できるように新しい可変値変数で受け直す var value = value
(1 ..< count).forEach { _ in
value += value }
return value }
![Page 72: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/72.jpg)
func function(initial value: Int, count: Int) -> Int {
return (1 ..< count).reduce(value) { value, _ in
value + value
}
}
![Page 73: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/73.jpg)
func function(let initial value: Int, count: Int) -> Int {
return (1 ..< count).reduce(value) { value, _ in
value + value
}
}
![Page 74: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/74.jpg)
// if var 構文で使用可能 if var value = optionalValue { … }
// while 構文で使用可能 while var value = iterator.next() { … }
// for 構文で使用可能 for var value in values { … }
// switch 構文で使用可能 switch optionalValue { case .some(var value): … case .none: … }
![Page 75: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/75.jpg)
// guard var 構文が使用可能 guard var value = optionalValue else {
fatalError() }
// これ以降で可変値変数 value が使える value += 200
![Page 76: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/76.jpg)
![Page 77: Swift 3.0 の新しい機能(のうちの9つ)](https://reader033.vdocuments.pub/reader033/viewer/2022051521/587123af1a28abe4448b5a4f/html5/thumbnails/77.jpg)