Slide 1

Slide 1 text

ࡓ͑ͳ͍൴ঁͷҭͯํ userInfo for potatotips #39

Slide 2

Slide 2 text

override init() { super.init() emplyedBy = "MAGES. 5pb. Game div" job = "iOS Developer" twitter = "@lovee" github = "el-hoshino" additionalInfo = "Euro Truck Simulator 2 ROCKS!!!" class Speaker: Producer { } }

Slide 3

Slide 3 text

userInfo: Dictionary ▸ UIKit Ͱଟ͘ͷ৔ॴͰ׆༻͞ΕΔ ▸ Notification ▸ NSError ▸ ͦͷଞଟ͘ͷΦϒδΣΫτΛލ͙σʔλͷ΍ΓऔΓ ▸ ͦΕނͷສೳ͕͋͞Δ ▸ userInfo ͸୯ͳΔश׳্ͷม਺໊ ▸ جຊ [String: Any] ͔ [Hashable: Any] ͷࣙॻܕ ▸ ౉͢σʔλ͸ܕʹറΒΕͳ͍

Slide 4

Slide 4 text

ͦΜͳ userInfo ͸ࡓ͑ͯͳ͍ʂ

Slide 5

Slide 5 text

ͦΜͳ userInfo ͸ࡓ͑ͯͳ͍ʂ ▸ Key ͷܕ͕ Stringʢ͔ AnyHashableʣ ▸ ଐੑ औΕΔ஋ ͕ϒϨΔ ఆ · Β ͳ ͍ ΜͩΑʂ ▸ Value ͷܕ͕ Any ▸ ʢຊདྷͷʣΩϟϥ ܕ ৘ ใ ͕ࢮΜͰΔΜͩΑʂ

Slide 6

Slide 6 text

ϢʔβϩάΠϯ௨஌Λߟ͑ͯΈΑ͏

Slide 7

Slide 7 text

ϢʔβϩάΠϯ௨஌ʢൃ৴ଆʣ let name = Notification .Name(rawValue: "PlayerLoggedin") let userInfo: [String: Any] = [ "id": 3891, "nickname": "Ճ౻ܙ" ] let notification = Notification (name: name, object: nil, userInfo: userInfo) NotificationCenter.default.post(notification)

Slide 8

Slide 8 text

ϢʔβϩάΠϯ௨஌ʢൃ৴ଆʣ let name = Notification .Name(rawValue: "PlayerLoggedin") let userInfo: [String: Any] = [ "id": 3891, "nickname": "Ճ౻ܙ" ] let notification = Notification (name: name, object: nil, userInfo: userInfo) NotificationCenter.default.post(notification) औΓಘΔΩʔ͕ར༻͢Δଆ͕Θ͔Βͳ͍ ࣙॻ಺ͷܕ৘ใ͕ͳ͘ͳΔ

Slide 9

Slide 9 text

ϢʔβϩάΠϯ௨஌ʢड৴ଆʣ func didReceiveNotification( _ notification: Notification) { guard let userInfo = notification.userInfo else { return } if let id = userInfo["id"] as? Int, let name = userInfo["nickname"] as? String { print(id) print(name) } else if let token = userInfo["token"] as? String { print(token) } }

Slide 10

Slide 10 text

ϢʔβϩάΠϯ௨஌ʢड৴ଆʣ func didReceiveNotification( _ notification: Notification) { guard let userInfo = notification.userInfo else { return } if let id = userInfo["id"] as? Int, let name = userInfo["nickname"] as? String { print(id) print(name) } else if let token = userInfo["token"] as? String { print(token) } } ΩʔΛࣗྗͰ֮͑Δඞཁ͕͋Δ ಛఆͳܕʹΩϟετμ΢ϯ͢Δඞཁ͕͋Δ if จ͸໢ཏͰ͖Δอূ͕ͳ͍

Slide 11

Slide 11 text

Զ͕࡞ͬͨϓϩάϥϜ͔ͩΒ
 ͲΜͳ΋ͷ͕͋Δ͔͘Β͍
 Զ͸஌ͬͯΔ!

Slide 12

Slide 12 text

Զ͕࡞ͬͨϓϩάϥϜ͔ͩΒ
 ͲΜͳ΋ͷ͕͋Δ͔͘Β͍
 Զ͸஌ͬͯΔ! ͓લ 6 ϲ݄લͷՇͷ໊લΛ
 ࠓ͙͢શ෦ݴ͑Δ͔ʁ

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

ྑ͍ઃܭ͸ޡͬͨ༻๏Λ ίϯύΠϧΤϥʔʹ͢Δ by ੴ઒ ༸ࢿ ʢגࣜձࣾϝϧΧϦʣ

Slide 15

Slide 15 text

userInfo Λϓϩσϡʔε͠Α͏ վ ળ

Slide 16

Slide 16 text

ϢʔβϩάΠϯ௨஌ʢڞ௨ʣ extension Notification { enum Player { case normal(id: Int, nickname: String) case anonymous(token: String) } } extension Notification.Player { static let key = "Player" }

Slide 17

Slide 17 text

ϢʔβϩάΠϯ௨஌ʢൃ৴ଆʣ extension Notification { enum Player { case normal(id: Int, nickname: String) case anonymous(token: String) } } extension Notification.Player { static let key = "Player" } let player = Notification.Player .normal(id: 3891, nickname: "Ճ౻ܙ") let userInfo = [Notification.Player.key: player] ͲΜͳΩʔ͕͋Δͷ͔΋͙͢Θ͔Δ ม਺಺ͷܕ৘ใ͕࢒Δ

Slide 18

Slide 18 text

ϢʔβϩάΠϯ௨஌ʢड৴ଆʣ extension Notification { enum Player { case normal(id: Int, nickname: String) case anonymous(token: String) } } extension Notification.Player { static let key = "Player" } guard let player = notification .userInfo?[Notification.Player.key] as? Notification.Player else { return } switch player { case .normal(id: let id, nickname: let name): print(id) print(name) case .anonymous(token: let token): print(token) } ΩʔΛ֮͑Δඞཁ͕ͳ͍ ܕΛΩϟετμ΢ϯ͢Δඞཁ͕ͳ͍ switch case จʹΑΔ໢ཏੑͷอূ

Slide 19

Slide 19 text

݁࿦ userInfo Λ [String: Any] ͷ··Ͱར༻ͤͣ ͳΔ΂͘Ұͭͷఆ·ͬͨܕʹམͱ͠ࠐ΋͏ enum Ͱ΋ struct Ͱ΋ Set Ͱ΋

Slide 20

Slide 20 text

୭΋͕ΩϡϯΩϡϯ͢ΔϝΠϯώϩΠϯʹɻ Ұ ໨ ྎ વ u s e r I n f o