Slide 1

Slide 1 text

ϝϞϦʔ؅ཧͷඒຯ͍͠ όΠΩϯάྉཧ Ray Fix @rayfix ը૾ఏڙ Yumi Kimura (CC2)

Slide 2

Slide 2 text

Ray Fix͸୭ʁ Source: https://en.wikipedia.org/wiki/La_Jolla

Slide 3

Slide 3 text

RayWenderlich.com

Slide 4

Slide 4 text

iosdc.jp Ray Fix

Slide 5

Slide 5 text

iosdc.jp Ray Fix

Slide 6

Slide 6 text

iosdc.jp Ray Fix ϝϞϦʔ؅ཧ

Slide 7

Slide 7 text

iosdc.jp Ray Fix ελοΫvsώʔϓྖҬ

Slide 8

Slide 8 text

iosdc.jp Ray Fix ελοΫ ελοΫ͸ͱʹ͔͘଎͍ʂ

Slide 9

Slide 9 text

iosdc.jp Ray Fix ελοΫ ελοΫ͸ͱʹ͔͘଎͍ʂ func candy() { let ramune = 10 let pocky = 12 let gummi = 20 }

Slide 10

Slide 10 text

iosdc.jp Ray Fix ελοΫ ελοΫ͸ͱʹ͔͘଎͍ʂ func candy() { let ramune = 10 let pocky = 12 let gummi = 20 } ελοΫ

Slide 11

Slide 11 text

iosdc.jp Ray Fix ελοΫ ελοΫ͸ͱʹ͔͘଎͍ʂ func candy() { let ramune = 10 let pocky = 12 let gummi = 20 } ελοΫ 10 12 20

Slide 12

Slide 12 text

iosdc.jp Ray Fix ελοΫ ελοΫ͸ͱʹ͔͘଎͍ʂ func candy() { let ramune = 10 let pocky = 12 let gummi = 20 } ελοΫ

Slide 13

Slide 13 text

iosdc.jp Ray Fix ελοΫ ελοΫ͸ͱʹ͔͘଎͍ʂ func candy() { let ramune = 10 let pocky = 12 let gummi = 20 } ελοΫ ϩοΫ͢Δඞཁ͸શ͘ͳ͍

Slide 14

Slide 14 text

iosdc.jp Ray Fix ώʔϓྖҬ class Dishɹ{ var name: String init(name: String) { self.name = name print(" Dish \(name)") } deinit { print("☠ Dish \(name)") } }

Slide 15

Slide 15 text

iosdc.jp Ray Fix ώʔϓྖҬ

Slide 16

Slide 16 text

iosdc.jp Ray Fix ώʔϓྖҬ let softCream = Dish(name: “Soft Cream")

Slide 17

Slide 17 text

iosdc.jp Ray Fix ώʔϓྖҬ let softCream = Dish(name: “Soft Cream") Count: 1 “Soft Cream” ώʔϓ

Slide 18

Slide 18 text

iosdc.jp Ray Fix ώʔϓྖҬ let softCream = Dish(name: “Soft Cream") Count: 1 “Soft Cream” ώʔϓ softCream ελοΫ

Slide 19

Slide 19 text

iosdc.jp Ray Fix ώʔϓྖҬ let softCream = Dish(name: “Soft Cream") Count: 1 “Soft Cream” softCream ώʔϓ ελοΫ

Slide 20

Slide 20 text

iosdc.jp Ray Fix ώʔϓྖҬ let softCream = Dish(name: “Soft Cream") let special = softCream Count: 1 “Soft Cream” softCream ώʔϓ ελοΫ

Slide 21

Slide 21 text

iosdc.jp Ray Fix ώʔϓྖҬ let softCream = Dish(name: “Soft Cream") let special = softCream Count: 1 “Soft Cream” softCream ώʔϓ ελοΫ Count: 2 “Soft Cream” special

Slide 22

Slide 22 text

iosdc.jp Ray Fix ڞ༗ softCream.name = "Choco Soft Cream" special.name // "Choco Soft Cream"

Slide 23

Slide 23 text

iosdc.jp Ray Fix ڞ༗ softCream.name = "Choco Soft Cream" special.name // "Choco Soft Cream" ϫΦɺศརʂʂʂ

Slide 24

Slide 24 text

iosdc.jp Ray Fix ڞ༗ softCream.name = "Choco Soft Cream" special.name // "Choco Soft Cream" ϫΦɺศརʂʂʂ ͔͠͠

Slide 25

Slide 25 text

iosdc.jp Ray Fix ڞ༗ softCream.name = "Choco Soft Cream" special.name // "Choco Soft Cream" ϫΦɺศརʂʂʂ ͔͠͠ special.name = "ϐʔϚϯ" softCream.name // "ϐʔϚϯ"

Slide 26

Slide 26 text

iosdc.jp Ray Fix ڞ༗ softCream.name = "Choco Soft Cream" special.name // "Choco Soft Cream" ϫΦɺศརʂʂʂ ͔͠͠ special.name = "ϐʔϚϯ" softCream.name // "ϐʔϚϯ" ૝ఆ֎ͷڞ༗͸ࢀরܕͷܽ఺

Slide 27

Slide 27 text

iosdc.jp Ray Fix ղܾ๏:ఆ਺Λ࢖͏ class Dishɹ{ init(name: String) { self.name = name print(" Dish \(name)") } deinit { print("☠ Dish \(name)") } } var name: String

Slide 28

Slide 28 text

iosdc.jp Ray Fix ղܾ๏:ఆ਺Λ࢖͏ class Dishɹ{ init(name: String) { self.name = name print(" Dish \(name)") } deinit { print("☠ Dish \(name)") } } var name: String

Slide 29

Slide 29 text

iosdc.jp Ray Fix ղܾ๏:ఆ਺Λ࢖͏ class Dishɹ{ init(name: String) { self.name = name print(" Dish \(name)") } deinit { print("☠ Dish \(name)") } } let name: String

Slide 30

Slide 30 text

iosdc.jp Ray Fix ղܾ๏:ఆ਺Λ࢖͏ class Dishɹ{ init(name: String) { self.name = name print(" Dish \(name)") } deinit { print("☠ Dish \(name)") } } let name: String special.name = "ϐʔϚϯ"

Slide 31

Slide 31 text

iosdc.jp Ray Fix ώʔϓྖҬ let softCream = Dish(name: “Soft Cream") let special = softCream Count: 0 “Soft Cream” ώʔϓ ελοΫ Count: 2 “Soft Cream” softCream special

Slide 32

Slide 32 text

iosdc.jp Ray Fix ώʔϓྖҬ let softCream = Dish(name: “Soft Cream") let special = softCream Count: 0 “Soft Cream” ώʔϓ ελοΫ

Slide 33

Slide 33 text

iosdc.jp Ray Fix ώʔϓྖҬ let softCream = Dish(name: “Soft Cream") let special = softCream ώʔϓ ελοΫ

Slide 34

Slide 34 text

iosdc.jp Ray Fix Customer orders ໰୊ɿ॥؀ࢀর Order customer dish Dish name

Slide 35

Slide 35 text

iosdc.jp Ray Fix ࢀরαΠΫϧ class Customer { var orders: [Order] func add(order: Order) { order.customer = self orders.append(order) } } class Order { var customer: Customer? let dish: Dish }

Slide 36

Slide 36 text

iosdc.jp Ray Fix ࢀরαΠΫϧ class Customer { var orders: [Order] func add(order: Order) { order.customer = self orders.append(order) } } class Order { var customer: Customer? let dish: Dish }

Slide 37

Slide 37 text

iosdc.jp Ray Fix ࢀরαΠΫϧ let ray = Customer(name: "Ray") let saikoro = Dish(name: “Saikoro Steak") let order = Order(dish: saikoro) ray.add(order: order)

Slide 38

Slide 38 text

iosdc.jp Ray Fix ࢀরαΠΫϧ let ray = Customer(name: "Ray") let saikoro = Dish(name: “Saikoro Steak") let order = Order(dish: saikoro) ray.add(order: order) ray saikoro order orders

Slide 39

Slide 39 text

iosdc.jp Ray Fix ࢀরαΠΫϧ let ray = Customer(name: "Ray") let saikoro = Dish(name: “Saikoro Steak") let order = Order(dish: saikoro) ray.add(order: order) ray saikoro order orders

Slide 40

Slide 40 text

iosdc.jp Ray Fix Xcode 8 Memory Visualizer

Slide 41

Slide 41 text

iosdc.jp Ray Fix Xcode 8 Memory Visualizer

Slide 42

Slide 42 text

iosdc.jp Ray Fix Xcode 8 Memory Visualizer

Slide 43

Slide 43 text

iosdc.jp Ray Fix Xcode 8 Memory Visualizer

Slide 44

Slide 44 text

iosdc.jp Ray Fix Xcode 8 Memory Visualizer

Slide 45

Slide 45 text

iosdc.jp Ray Fix Customer orders ॥؀ࢀর Order customer dish Dish name

Slide 46

Slide 46 text

iosdc.jp Ray Fix ॥؀ࢀর class Customer { var orders: [Order] func add(order: Order) { order.customer = self orders.append(order) } } class Order { let dish: Dish } var customer: Customer?

Slide 47

Slide 47 text

iosdc.jp Ray Fix ॥؀ࢀর class Customer { var orders: [Order] func add(order: Order) { order.customer = self orders.append(order) } } class Order { let dish: Dish } var customer: Customer? weak

Slide 48

Slide 48 text

iosdc.jp Ray Fix ॥؀ࢀর class Customer { var orders: [Order] func add(order: Order) { order.customer = self orders.append(order) } } class Order { let dish: Dish } var customer: Customer? weak

Slide 49

Slide 49 text

iosdc.jp Ray Fix ॥؀ࢀর let ray = Customer(name: "Ray") let saikoro = Dish(name: "Saikoro") let order = Order(dish: saikoro) ray.add(order: order) ray saikoro order orders

Slide 50

Slide 50 text

iosdc.jp Ray Fix ॥؀ࢀর let ray = Customer(name: "Ray") let saikoro = Dish(name: "Saikoro") let order = Order(dish: saikoro) ray.add(order: order) ray saikoro order orders

Slide 51

Slide 51 text

iosdc.jp Ray Fix ॥؀ࢀর let ray = Customer(name: "Ray") let saikoro = Dish(name: "Saikoro") let order = Order(dish: saikoro) ray.add(order: order) saikoro order nil

Slide 52

Slide 52 text

iosdc.jp Ray Fix ॥؀ࢀর let ray = Customer(name: "Ray") let saikoro = Dish(name: "Saikoro") let order = Order(dish: saikoro) ray.add(order: order) saikoro

Slide 53

Slide 53 text

iosdc.jp Ray Fix ॥؀ࢀর let ray = Customer(name: "Ray") let saikoro = Dish(name: "Saikoro") let order = Order(dish: saikoro) ray.add(order: order)

Slide 54

Slide 54 text

iosdc.jp Ray Fix weak ࢀর̍ ࢀর̎ ࢀর̏

Slide 55

Slide 55 text

iosdc.jp Ray Fix weak ࢀর̍ ࢀর̎ ࢀর̏

Slide 56

Slide 56 text

iosdc.jp Ray Fix weak ࢀর̍ ࢀর̎ ࢀর̏

Slide 57

Slide 57 text

iosdc.jp Ray Fix weak nil nil nil ࢀর̍ ࢀর̎ ࢀর̏

Slide 58

Slide 58 text

iosdc.jp Ray Fix weak nil nil nil ࢀর̍ ࢀর̎ ࢀর̏ reference value ࢀর̍ nil ࢀর̎ nil ࢀর̏ nil άϩʔόϧςʔϒϧ

Slide 59

Slide 59 text

iosdc.jp Ray Fix Swift weak 1 3 ࢀর̎ ࢀর̏ ࢀর̍

Slide 60

Slide 60 text

iosdc.jp Ray Fix Swift weak 1 3 ࢀর̎ ࢀর̏ ࢀর̍

Slide 61

Slide 61 text

iosdc.jp Ray Fix Swift weak 1 3 0 κϯϏ 3 ࢀর̎ ࢀর̏ ࢀর̍

Slide 62

Slide 62 text

ڧ͍ࢀরΧ΢ϯτ͕̌ʹ

Slide 63

Slide 63 text

ڧ͍ࢀরΧ΢ϯτ͕̌ʹ

Slide 64

Slide 64 text

ऑ͍ࢀরΧ΢ϯτ͕̌ʹ

Slide 65

Slide 65 text

ऑ͍ࢀরΧ΢ϯτ͕̌ʹ

Slide 66

Slide 66 text

unowned • unowned ͸ผͷछྨͷऑ͍ࢀর • ࢀরઌ͕ඞͣ͋Δ͜ͱ͕લఏ • ΋͠ࢀরઌ͕ͳ͍ͱɺϓϩάϥϜ͕ఀࢭ͢Δ

Slide 67

Slide 67 text

unowned class Order { let dish: Dish } class Customer { var orders: [Order] func add(dish: Dish) { let order = Order(dish: dish, customer: self) orders.append(order) } } let customer: Customer

Slide 68

Slide 68 text

unowned class Order { let dish: Dish } class Customer { var orders: [Order] func add(dish: Dish) { let order = Order(dish: dish, customer: self) orders.append(order) } } let customer: Customer

Slide 69

Slide 69 text

unowned class Order { let dish: Dish } class Customer { var orders: [Order] func add(dish: Dish) { let order = Order(dish: dish, customer: self) orders.append(order) } } let customer: Customer unowned

Slide 70

Slide 70 text

unowned class Order { let dish: Dish } class Customer { var orders: [Order] func add(dish: Dish) { let order = Order(dish: dish, customer: self) orders.append(order) } } let customer: Customer unowned

Slide 71

Slide 71 text

iosdc.jp Ray Fix unowned 1 3 0 κϯϏ 3 ࢀর̎ ࢀর̏ ࢀর̍

Slide 72

Slide 72 text

iosdc.jp Ray Fix unowned 1 3 0 κϯϏ 3 ࢀর̎ ࢀর̏ ࢀর̍ ϓϩάϥϜ͕ఀࢭ

Slide 73

Slide 73 text

ؔ਺ͱΫϩʔδϟ΋ࢀরܕ { }

Slide 74

Slide 74 text

ؔ਺ͱΫϩʔδϟ΋ࢀরܕ { }

Slide 75

Slide 75 text

ྫɿण࢘԰ enum MenuItem: String { case toro, ebi, anago, uni, ikura, hamachi } typealias Action = ()->()

Slide 76

Slide 76 text

ྫɿ ण࢘԰ class Sushiya { lazy var menu: [MenuItem: Action] = […] func prepare(_ menuItem: MenuItem) { menu[menuItem]?() } private func serve(dish: Dish) { print("Now serving \(dish.name)") } }

Slide 77

Slide 77 text

ྫɿ ण࢘԰ menu = [ .toro: { let dish = Dish(name: "Toro") }, … ] serve(dish: dish)

Slide 78

Slide 78 text

ྫɿ ण࢘԰ menu = [ .toro: { let dish = Dish(name: "Toro") }, … ] serve(dish: dish) self.

Slide 79

Slide 79 text

ྫɿ ण࢘԰ menu = [ .toro: { let dish = Dish(name: "Toro") }, … ] serve(dish: dish) self. let sushiya = Sushiya() sushiya.prepare(.toro)

Slide 80

Slide 80 text

ϝϞϦʔ࿙Ε

Slide 81

Slide 81 text

ղܾ๏ɿ ΩϟϓνϟʔϦετ var value = 0 let showValue = { print(value) }

Slide 82

Slide 82 text

ղܾ๏ɿ ΩϟϓνϟʔϦετ var value = 0 let showValue = { print(value) } showValue() // prints 0 value = 10 showValue() // prints 10

Slide 83

Slide 83 text

ղܾ๏ɿ ΩϟϓνϟʔϦετ var value = 0 let showValue = { print(value) } showValue() // prints 0 value = 10 showValue() // prints 10

Slide 84

Slide 84 text

ղܾ๏ɿ ΩϟϓνϟʔϦετ var value = 0 let showValue = { print(value) } showValue() // prints 0 value = 10 showValue() // prints [value] in 10

Slide 85

Slide 85 text

ղܾ๏ɿ ΩϟϓνϟʔϦετ var value = 0 let showValue = { print(value) } showValue() // prints 0 value = 10 showValue() // prints [value] in 0

Slide 86

Slide 86 text

ղܾ๏ɿ ΩϟϓνϟʔϦετ menu = [ .toro: { let dish = Dish(name: "Toro") self.serve(dish: dish) }, … ]

Slide 87

Slide 87 text

ղܾ๏ɿ ΩϟϓνϟʔϦετ menu = [ .toro: { let dish = Dish(name: "Toro") self.serve(dish: dish) }, … ] [unowned self] in #

Slide 88

Slide 88 text

ඇಉظͷ໰୊ private func serve(dish: Dish) { print("Now serving \(dish.name)") }

Slide 89

Slide 89 text

ඇಉظͷ໰୊ private func serve(dish: Dish) { DispatchQueue.main.async { print("Now serving \(dish.name)") self.served += 1 } } private func serve(dish: Dish) { print("Now serving \(dish.name)") }

Slide 90

Slide 90 text

ඇಉظͷ໰୊ [unowned self] in private func serve(dish: Dish) { DispatchQueue.main.async { print("Now serving \(dish.name)") self.served += 1 } } private func serve(dish: Dish) { print("Now serving \(dish.name)") }

Slide 91

Slide 91 text

ඇಉظͷ໰୊ [unowned self] in private func serve(dish: Dish) { DispatchQueue.main.async { print("Now serving \(dish.name)") self.served += 1 } } private func serve(dish: Dish) { print("Now serving \(dish.name)") } CRASH!!!!

Slide 92

Slide 92 text

ඇಉظͷ໰୊ private func serve(dish: Dish) { DispatchQueue.main.async { print("Now serving \(dish.name)") self.served += 1 } } private func serve(dish: Dish) { print("Now serving \(dish.name)") }

Slide 93

Slide 93 text

weak private func serve(dish: Dish) { DispatchQueue.main.async { [weak self] in print("Now serving \(dish.name)") self?.served += 1 } }

Slide 94

Slide 94 text

strong weak μϯε private func serve(dish: Dish) { DispatchQueue.main.async { [weak self] in guard let strongSelf = self else { print("Cancelled \(dish.name)") return } print("Now serving \(dish.name)") strongSelf.served += 1 } }

Slide 95

Slide 95 text

ඒຯ͍͠SWIFTΛॻ͘ʹ͸ ϝϞϦʔͱͷ૬ੑΛ ೺Ѳ͢Δඞཁ͕͋Δ

Slide 96

Slide 96 text

͝ਗ਼ௌ͋Γ͕ͱ͏ ͍͟͝·ͨ͠

Slide 97

Slide 97 text

https://www.youtube.com/watch?v=0-vWT-t0UHg ԣ඿ϦφοΫεಡॻձ̒̓ɿmalloc https://www.mikeash.com/pyblog/friday-qa-2015-12-11-swift-weak-references.html SWIFT ऑ͍ࢀরͷৄ͍͠࿩ http://bit.ly/etystamp ༑ୡͷ LINE ελϜϓʂ https://en.wikipedia.org/wiki/La_Jolla ೆΧϦϑΥϧχΞͷࣸਅ https://ja.wikipedia.org/wiki/ण࢘ ण࢘ͷࣸਅ http://echo-labs.com/ ECHO LABS ݦඍڸ http://raywenderlich.com/ Ray Wenderlich ϝϞϦʔνϡʔτ (Maxime Defauwஶऀ) 9݄ެ։༧ఆ ࢀߟϦϯΫ https://github.com/rayfix/MemoryDish ιʔείʔυɺεϥΠυ wwdc2016/416/ WWDC Swift Performance

Slide 98

Slide 98 text

https://www.youtube.com/watch?v=0-vWT-t0UHg ԣ඿ϦφοΫεಡॻձ̒̓ɿmalloc https://www.mikeash.com/pyblog/friday-qa-2015-12-11-swift-weak-references.html SWIFT ऑ͍ࢀরͷৄ͍͠࿩ http://bit.ly/etystamp ༑ୡͷ LINE ελϜϓʂ https://en.wikipedia.org/wiki/La_Jolla ೆΧϦϑΥϧχΞͷࣸਅ https://ja.wikipedia.org/wiki/ण࢘ ण࢘ͷࣸਅ http://echo-labs.com/ ECHO LABS ݦඍڸ http://raywenderlich.com/ Ray Wenderlich ϝϞϦʔνϡʔτ (Maxime Defauwஶऀ) 9݄ެ։༧ఆ ࢀߟϦϯΫ https://github.com/rayfix/MemoryDish ιʔείʔυɺεϥΠυ wwdc2016/416/ WWDC Swift Performance