Slide 1

Slide 1 text

AnyܕΛprotocolʹΩϟετ͢Δ࣌ʹ ؾΛ͚ͭͨํ͕͍͍࿩ 2019/5/21 ͋Δ͋ΔLT vol4 PKPK-Carnage(Tomosuke Okada) 1

Slide 2

Slide 2 text

About me • iOSྺ2೥ͪΐͬͱ • Qiita → @PKPK-Carnage • Github → PKPK-Carnage • Twitter → @PKPK-Carnage

Slide 3

Slide 3 text

ͦΜͳͷ؆୯͡ΌΜ 3

Slide 4

Slide 4 text

Any as? Protocol 4

Slide 5

Slide 5 text

ྫɿNotification 5 protocol Animal {} struct Dog: Animal {}

Slide 6

Slide 6 text

ྫɿNotification 6 class HogeClass { func postNotification() { // DogΛૹΔ NotificationCenter.default.post( name: .init("Animal"), object: Dog() ) } }

Slide 7

Slide 7 text

ྫɿNotification 7 class FugaClass { init() { NotificationCenter.default.addObserver( self, selector: #selector(self.didReceivedNotification(_:)), name: .init("Animal"), object: nil ) } @objc func didReceivedNotification(_ notification: Notification) { } }

Slide 8

Slide 8 text

ྫɿNotification 8 @objc func didReceivedNotification(_ notification: Notification) { // notification.objectͷத਎Λग़ྗ͢Δ print("\(String(describing: notification.object))") // notification.objectΛAnimal(Protocol)ʹΩϟετͨ͠ޙͷத਎Λग़ྗ͢Δ let animal = notification.object as? Animal print("\(String(describing: animal))") } Optional(__lldb_expr_11.Dog()) nil

Slide 9

Slide 9 text

9

Slide 10

Slide 10 text

SR-3871 10

Slide 11

Slide 11 text

SR-3871 • Swiftͷstructʹas AnyObjectΛߦͬͯɺAnyܕʹ֨ೲͨ࣌͠ʹɺ Protocol΁ͷ४ڌ൑ఆ͕ߦ͑ͳ͘ͳΔόά͕͋Δ • AnyܕͷΦϒδΣΫτʹas AnyObjectΛߦ͑͹ɺͪΌΜͱProtocol΁ ͷ४ڌ൑ఆ͕ߦ͑ΔΑ͏ʹͳΔɻ 11

Slide 12

Slide 12 text

࣮ࡍʹ΍ͬͯΈͨ 12 @objc func didReceivedNotification(_ notification: Notification) { // notification.objectͷத਎Λग़ྗ͢Δ print("\(String(describing: notification.object))") // notification.objectΛAnimal(Protocol)ʹΩϟετͨ͠ޙͷத਎Λग़ྗ͢Δ let animal = notification.object as? Animal print("\(String(describing: animal))") }

Slide 13

Slide 13 text

࣮ࡍʹ΍ͬͯΈͨ 13 @objc func didReceivedNotification(_ notification: Notification) { // notification.objectͷத਎Λग़ྗ͢Δ print("\(String(describing: notification.object))") // notification.objectΛAnimal(Protocol)ʹΩϟετͨ͠ޙͷத਎Λग़ྗ͢Δ let animal = (notification.object as AnyObject) as? Animal print("\(String(describing: animal))") }

Slide 14

Slide 14 text

࣮ࡍʹ΍ͬͯΈͨ 14 Optional(__lldb_expr_11.Dog()) Optional(__lldb_expr_11.Dog()) @objc func didReceivedNotification(_ notification: Notification) { // notification.objectͷத਎Λग़ྗ͢Δ print("\(String(describing: notification.object))") // notification.objectΛAnimal(Protocol)ʹΩϟετͨ͠ޙͷத਎Λग़ྗ͢Δ let animal = (notification.object as AnyObject) as? Animal print("\(String(describing: animal))") }

Slide 15

Slide 15 text

15

Slide 16

Slide 16 text

Qiitaॻ͖·ͨ͠ 16 https://qiita.com/PKPK-Carnage/items/5e2eda7735aa24f807a9

Slide 17

Slide 17 text

͓ΘΓ 17