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

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

Elvis Shi
April 13, 2017

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

Elvis Shi

April 13, 2017
Tweet

More Decks by Elvis Shi

Other Decks in Programming

Transcript

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

    View Slide

  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 {
    }
    }

    View Slide

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

    View Slide

  4. ͦΜͳ userInfo ͸ࡓ͑ͯͳ͍ʂ

    View Slide

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

    View Slide

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

    View Slide

  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)

    View Slide

  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)
    औΓಘΔΩʔ͕ར༻͢Δଆ͕Θ͔Βͳ͍
    ࣙॻ಺ͷܕ৘ใ͕ͳ͘ͳΔ

    View Slide

  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)
    }
    }

    View Slide

  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 จ͸໢ཏͰ͖Δอূ͕ͳ͍

    View Slide

  11. Զ͕࡞ͬͨϓϩάϥϜ͔ͩΒ

    ͲΜͳ΋ͷ͕͋Δ͔͘Β͍

    Զ͸஌ͬͯΔ!

    View Slide

  12. Զ͕࡞ͬͨϓϩάϥϜ͔ͩΒ

    ͲΜͳ΋ͷ͕͋Δ͔͘Β͍

    Զ͸஌ͬͯΔ!
    ͓લ 6 ϲ݄લͷՇͷ໊લΛ

    ࠓ͙͢શ෦ݴ͑Δ͔ʁ

    View Slide

  13. View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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]
    ͲΜͳΩʔ͕͋Δͷ͔΋͙͢Θ͔Δ
    ม਺಺ͷܕ৘ใ͕࢒Δ

    View Slide

  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 จʹΑΔ໢ཏੑͷอূ

    View Slide

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

    View Slide

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

    View Slide