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

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

Avatar for Elvis Shi Elvis Shi
April 13, 2017

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

Avatar for Elvis Shi

Elvis Shi

April 13, 2017
Tweet

More Decks by Elvis Shi

Other Decks in Programming

Transcript

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

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

    औΕΔ஋ ͕ϒϨΔ ఆ · Β ͳ ͍ ΜͩΑʂ ▸ Value ͷܕ͕ Any ▸ ʢຊདྷͷʣΩϟϥ ܕ ৘ ใ ͕ࢮΜͰΔΜͩΑʂ
  4. ϢʔβϩάΠϯ௨஌ʢൃ৴ଆʣ 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)
  5. ϢʔβϩάΠϯ௨஌ʢൃ৴ଆʣ 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) औΓಘΔΩʔ͕ར༻͢Δଆ͕Θ͔Βͳ͍ ࣙॻ಺ͷܕ৘ใ͕ͳ͘ͳΔ
  6. ϢʔβϩάΠϯ௨஌ʢड৴ଆʣ 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) } }
  7. ϢʔβϩάΠϯ௨஌ʢड৴ଆʣ 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 จ͸໢ཏͰ͖Δอূ͕ͳ͍
  8. ϢʔβϩάΠϯ௨஌ʢڞ௨ʣ extension Notification { enum Player { case normal(id: Int,

    nickname: String) case anonymous(token: String) } } extension Notification.Player { static let key = "Player" }
  9. ϢʔβϩάΠϯ௨஌ʢൃ৴ଆʣ 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] ͲΜͳΩʔ͕͋Δͷ͔΋͙͢Θ͔Δ ม਺಺ͷܕ৘ใ͕࢒Δ
  10. ϢʔβϩάΠϯ௨஌ʢड৴ଆʣ 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 จʹΑΔ໢ཏੑͷอূ