Upgrade to Pro — share decks privately, control downloads, hide ads and more …

冴えない彼女(userInfo)の育て方

Af64bc38c0ffcfcabdf430759ee491ce?s=47 Elvis Shi
April 13, 2017

 冴えない彼女(userInfo)の育て方

Af64bc38c0ffcfcabdf430759ee491ce?s=128

Elvis Shi

April 13, 2017
Tweet

More Decks by Elvis Shi

Other Decks in Programming

Transcript

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

  2. 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 { } }
  3. userInfo: Dictionary ▸ UIKit Ͱଟ͘ͷ৔ॴͰ׆༻͞ΕΔ ▸ Notification ▸ NSError ▸

    ͦͷଞଟ͘ͷΦϒδΣΫτΛލ͙σʔλͷ΍ΓऔΓ ▸ ͦΕނͷສೳ͕͋͞Δ ▸ userInfo ͸୯ͳΔश׳্ͷม਺໊ ▸ جຊ [String: Any] ͔ [Hashable: Any] ͷࣙॻܕ ▸ ౉͢σʔλ͸ܕʹറΒΕͳ͍
  4. ͦΜͳ userInfo ͸ࡓ͑ͯͳ͍ʂ

  5. ͦΜͳ userInfo ͸ࡓ͑ͯͳ͍ʂ ▸ Key ͷܕ͕ Stringʢ͔ AnyHashableʣ ▸ ଐੑ

    औΕΔ஋ ͕ϒϨΔ ఆ · Β ͳ ͍ ΜͩΑʂ ▸ Value ͷܕ͕ Any ▸ ʢຊདྷͷʣΩϟϥ ܕ ৘ ใ ͕ࢮΜͰΔΜͩΑʂ
  6. ϢʔβϩάΠϯ௨஌Λߟ͑ͯΈΑ͏

  7. ϢʔβϩάΠϯ௨஌ʢൃ৴ଆʣ 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)
  8. ϢʔβϩάΠϯ௨஌ʢൃ৴ଆʣ 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) औΓಘΔΩʔ͕ར༻͢Δଆ͕Θ͔Βͳ͍ ࣙॻ಺ͷܕ৘ใ͕ͳ͘ͳΔ
  9. ϢʔβϩάΠϯ௨஌ʢड৴ଆʣ 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) } }
  10. ϢʔβϩάΠϯ௨஌ʢड৴ଆʣ 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 จ͸໢ཏͰ͖Δอূ͕ͳ͍
  11. Զ͕࡞ͬͨϓϩάϥϜ͔ͩΒ
 ͲΜͳ΋ͷ͕͋Δ͔͘Β͍
 Զ͸஌ͬͯΔ!

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

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

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

  16. ϢʔβϩάΠϯ௨஌ʢڞ௨ʣ extension Notification { enum Player { case normal(id: Int,

    nickname: String) case anonymous(token: String) } } extension Notification.Player { static let key = "Player" }
  17. ϢʔβϩάΠϯ௨஌ʢൃ৴ଆʣ 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] ͲΜͳΩʔ͕͋Δͷ͔΋͙͢Θ͔Δ ม਺಺ͷܕ৘ใ͕࢒Δ
  18. ϢʔβϩάΠϯ௨஌ʢड৴ଆʣ 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 จʹΑΔ໢ཏੑͷอূ
  19. ݁࿦ userInfo Λ [String: Any] ͷ··Ͱར༻ͤͣ ͳΔ΂͘Ұͭͷఆ·ͬͨܕʹམͱ͠ࠐ΋͏ enum Ͱ΋ struct

    Ͱ΋ Set<T> Ͱ΋
  20. ୭΋͕ΩϡϯΩϡϯ͢ΔϝΠϯώϩΠϯʹɻ Ұ ໨ ྎ વ u s e r I

    n f o