$30 off During Our Annual Pro Sale. View Details »

NSNotification.userInfoを
Swiftyに扱おう

takasek
March 16, 2016

 NSNotification.userInfoを
Swiftyに扱おう

potatotips #27 (http://connpass.com/event/27156/) の発表資料です。https://github.com/takasek/Notifwift の紹介です。

takasek

March 16, 2016
Tweet

More Decks by takasek

Other Decks in Technology

Transcript

  1. /4/PUJGJDBUJPOVTFS*OGPΛ

    4XJGUZʹѻ͓͏
    QPUBUPUJQT
    CZ

    View Slide

  2. !UBLBTFL
    w GSFFMBODFJ04&OHJOFFS
    w ג
    'J/$ͷํ͔Βདྷ·ͨ͠
    w ɹ!UBLBTFL
    w ɹUBLBTFL
    w ɹUBLBTFL

    View Slide

  3. ಥવͰ͕͢
    /4/PUJpDBUJPO
    ͩΔ͍Ͱ͢Ͷ

    View Slide

  4. Α͋͘Δ࣮૷
    // Model
    class User { … }
    API.fetchUser(id: id) { (user: User) in
    NSNotificationCenter.defaultCenter()
    .postNotificationName(
    "DidUserReceivedNotification",
    object: nil,
    userInfo: ["user": user])
    }
    // View
    NSNotificationCenter.defaultCenter().addObserverForName(
    "DidUserReceivedNotification",
    object: nil,
    queue: NSOperationQueue.mainQueue(),
    usingBlock: { [weak self] (n: NSNotification) in
    guard let user = n.userInfo?["user"] as? User else {
    return
    }
    self?.reload(user)
    })

    View Slide

  5. Α͋͘Δ࣮૷
    // Model
    class User { … }
    API.fetchUser(id: id) { (user: User) in
    NSNotificationCenter.defaultCenter()
    .postNotificationName(
    "DidUserReceivedNotification",
    object: nil,
    userInfo: ["user": user])
    }
    // View
    NSNotificationCenter.defaultCenter().addObserverForName(
    "DidUserReceivedNotification",
    object: nil,
    queue: NSOperationQueue.mainQueue(),
    usingBlock: { [weak self] (n: NSNotification) in
    guard let user = n.userInfo?["user"] as? User else {
    return
    }
    self?.reload(user)
    })
    DMBTT6TFS͕͋Γ·ͯ͠ɺ

    View Slide

  6. Α͋͘Δ࣮૷
    // Model
    class User { … }
    API.fetchUser(id: id) { (user: User) in
    NSNotificationCenter.defaultCenter()
    .postNotificationName(
    "DidUserReceivedNotification",
    object: nil,
    userInfo: ["user": user])
    }
    // View
    NSNotificationCenter.defaultCenter().addObserverForName(
    "DidUserReceivedNotification",
    object: nil,
    queue: NSOperationQueue.mainQueue(),
    usingBlock: { [weak self] (n: NSNotification) in
    guard let user = n.userInfo?["user"] as? User else {
    return
    }
    self?.reload(user)
    })
    "1*͔Β
    6TFSΛऔಘͨ͠Βʜ

    View Slide

  7. Α͋͘Δ࣮૷
    // Model
    class User { … }
    API.fetchUser(id: id) { (user: User) in
    NSNotificationCenter.defaultCenter()
    .postNotificationName(
    "DidUserReceivedNotification",
    object: nil,
    userInfo: ["user": user])
    }
    // View
    NSNotificationCenter.defaultCenter().addObserverForName(
    "DidUserReceivedNotification",
    object: nil,
    queue: NSOperationQueue.mainQueue(),
    usingBlock: { [weak self] (n: NSNotification) in
    guard let user = n.userInfo?["user"] as? User else {
    return
    }
    self?.reload(user)
    })
    %JDUJPOBSZʹแΜͰ
    /PUJpDBUJPOͷVTFS*OGPʹ
    LFZࢦఆͯ͠ೖΕͯʜ

    View Slide

  8. Α͋͘Δ࣮૷
    // Model
    class User { … }
    API.fetchUser(id: id) { (user: User) in
    NSNotificationCenter.defaultCenter()
    .postNotificationName(
    "DidUserReceivedNotification",
    object: nil,
    userInfo: ["user": user])
    }
    // View
    NSNotificationCenter.defaultCenter().addObserverForName(
    "DidUserReceivedNotification",
    object: nil,
    queue: NSOperationQueue.mainQueue(),
    usingBlock: { [weak self] (n: NSNotification) in
    guard let user = n.userInfo?["user"] as? User else {
    return
    }
    self?.reload(user)
    })
    ͍ͦ͒

    View Slide

  9. Α͋͘Δ࣮૷
    // Model
    class User { … }
    API.fetchUser(id: id) { (user: User) in
    NSNotificationCenter.defaultCenter()
    .postNotificationName(
    "DidUserReceivedNotification",
    object: nil,
    userInfo: ["user": user])
    }
    // View
    NSNotificationCenter.defaultCenter().addObserverForName(
    "DidUserReceivedNotification",
    object: nil,
    queue: NSOperationQueue.mainQueue(),
    usingBlock: { [weak self] (n: NSNotification) in
    guard let user = n.userInfo?["user"] as? User else {
    return
    }
    self?.reload(user)
    })
    7JFXͰ
    /PUJpDBUJPOΛ
    ड͚ͨΒʜ

    View Slide

  10. Α͋͘Δ࣮૷
    // Model
    class User { … }
    API.fetchUser(id: id) { (user: User) in
    NSNotificationCenter.defaultCenter()
    .postNotificationName(
    "DidUserReceivedNotification",
    object: nil,
    userInfo: ["user": user])
    }
    // View
    NSNotificationCenter.defaultCenter().addObserverForName(
    "DidUserReceivedNotification",
    object: nil,
    queue: NSOperationQueue.mainQueue(),
    usingBlock: { [weak self] (n: NSNotification) in
    guard let user = n.userInfo?["user"] as? User else {
    return
    }
    self?.reload(user)
    })
    VTFS*OGP͔Β
    LFZࢦఆͯ͠
    6TFSΛऔΓग़ͯ͠

    View Slide

  11. Α͋͘Δ࣮૷
    // Model
    class User { … }
    API.fetchUser(id: id) { (user: User) in
    NSNotificationCenter.defaultCenter()
    .postNotificationName(
    "DidUserReceivedNotification",
    object: nil,
    userInfo: ["user": user])
    }
    // View
    NSNotificationCenter.defaultCenter().addObserverForName(
    "DidUserReceivedNotification",
    object: nil,
    queue: NSOperationQueue.mainQueue(),
    usingBlock: { [weak self] (n: NSNotification) in
    guard let user = n.userInfo?["user"] as? User else {
    return
    }
    self?.reload(user)
    })
    SFMPBE VTFS
    Ͱ
    7JFXΛߋ৽͠·͢

    View Slide

  12. /4/PUJpDBUJPOͷͩΔ͍ͱ͜
    // Model
    class User { … }
    API.fetchUser(id: id) { (user: User) in
    NSNotificationCenter.defaultCenter()
    .postNotificationName(
    "DidUserReceivedNotification",
    object: nil,
    userInfo: ["user": user])
    }
    // View
    NSNotificationCenter.defaultCenter().addObserverForName(
    "DidUserReceivedNotification",
    object: nil,
    queue: NSOperationQueue.mainQueue(),
    usingBlock: { [weak self] (n: NSNotification) in
    guard let user = n.userInfo?["user"] as? User else {
    return
    }
    self?.reload(user)
    })
    /4/PUJpDBUJPO$FOUFSͷ

    هड़͕৑௕
    VTFS*OGPͷ%JDUJPOBSZ,FZʹ

    ΦϨΦϨ,FZΛ࢖Θ͟ΔΛಘͳ͍

    ɹϦςϥϧͰVTFSͱ͔ॻ͖ͨ͘ͳ͍ʜ

    View Slide

  13. গ͠୤ઢ͠·͕͢ʜ
    4XJGUͰ͸
    ࢀরܕΑΓ஋ܕΛ࢖͓͏ͥʂ
    ͱΑ͘ݴΘΕ·͢

    View Slide

  14. ஋ܕͷ͍͍ͱ͜
    جຊతʹJNNVUBCMFͰঢ়ଶΛ࣋ͨͳ͍
    ࢀরΧ΢ϯτΛߟ͑Δඞཁ͕ͳ͍
    ܧঝΛߟ͑ͳ͍͍ͯ͘
    ڞ௨͢ΔৼΔ෣͍͸

    QSPUPDPMͰهड़͢Δ΄͏͕4XJGUZ

    View Slide

  15. ͡Ό͋
    ͜ͷίʔυͷ6TFSDMBTT΋
    ஋ܕʹ͠Α͏
    T U S V D U

    View Slide

  16. class User {
    let id: Int
    let name: String
    init(id: Int, name: String) {
    self.id = id
    self.name = name
    }
    }

    View Slide

  17. struct User {
    let id: Int
    let name: String
    }

    View Slide

  18. View Slide


  19. View Slide

  20. public class NSNotification : NSObject, NSCopying, NSCoding
    {

    public var userInfo: [NSObject : AnyObject]? { get }
    VTFS*OGP͸40CKFDU"OZ0CKFDU> ܕ
    "OZ0CKFDU͸DMBTT͔͠ड͚ೖΕͳ͍
    ͭ·Γɺ

    TUSVDU΋FOVN΋

    /4/PUJpDBUJPOVTFS*OGPͷ

    த਎ͱͯ͠࢖͑ͳ͍

    View Slide

  21. ΍͸Γ

    6TFSͷTUSVDUԽ͸
    ఘΊΔ͔͠ͳ͍ͷ͔ʜ

    View Slide

  22. ͍͍͑ɺఘΊͳ͍Ͱʜʂ
    ͋͞ɺTUSVDUΛ
    DMBTTͰϥοϓ͢ΔͷͰ͢ʜʂ

    View Slide

  23. struct User {
    let id: Int
    let name: String
    }
    API.fetchUser(id: id) { (user: User) in
    NSNotificationCenter.defaultCenter()
    .postNotificationName(
    "DidUserReceivedNotification",
    object: nil,
    userInfo: ["user": user])
    }
    // error!!
    ˢ͜͜஫໨ʂ

    View Slide

  24. struct User {
    let id: Int
    let name: String
    }
    class UserContainer {
    let user: User
    init(user: User) {
    self.user = user
    }
    }
    API.fetchUser(id: id) { (user: User) in
    NSNotificationCenter.defaultCenter()
    .postNotificationName(
    "DidUserReceivedNotification",
    object: nil,
    userInfo: ["user": UserContainer(user: user))
    }
    TUSVDU6TFSΛ
    DMBTT6TFS$POUBJOFSͰแΉ͜ͱͰ
    "OZ0CKFDUͱͯ͠ѻ͑ΔΑ͏ʹʂ

    View Slide

  25. Α͠
    ͜ΕͰ͍͚Δʂ

    View Slide

  26. ͍͚Δʜ͚Ͳ
    Πέͯͳ͍ʜ
    class UserContainer {
    let user: User
    init(user: User) {
    self.user = user
    }
    }

    View Slide

  27. /4/PUJpDBUJPOͷͩΔ͍ͱ͜BHBJO
    /4/PUJpDBUJPO$FOUFSͷ

    هड़͕৑௕
    VTFS*OGPͷ%JDUJPOBSZ,FZʹ

    ΦϨΦϨ,FZΛ࢖Θ͟ΔΛಘͳ͍
    VTFS*OGPͷ7BMVFͱͯ͠

    "OZ$MBTT͔͠ೖΕΒΕͳ͍

    View Slide

  28. ष͍΋ͷʹ͸֖Ͱ͢ʜʂ
    ͋͞ɺΠέͯͳ͍ॲཧ͸શ෦
    ϥΠϒϥϦʹԡ͠ࠐΊΔͷͰ͢ʜʂ

    View Slide

  29. /PUJGXJGUͰ

    /4/PUJGJDBUJPOVTFS*OGPΛ

    4XJGUZʹѻ͓͏

    View Slide

  30. ͜Ε͕ʜ
    // Model
    struct User { … }
    class PayloadContainer {
    let payload: Any
    init(payload: Any) { self.payload = payload }
    }
    API.fetchUser(id: id) { (user: User) in
    NSNotificationCenter.defaultCenter()
    .postNotificationName(
    "DidUserReceivedNotification",
    object: nil,
    userInfo: ["user": PayloadContainer(payload: user))
    }
    // View
    NSNotificationCenter.defaultCenter().addObserverForName(
    "DidUserReceivedNotification",
    object: nil,
    queue: NSOperationQueue.mainQueue(),
    usingBlock: { [weak self] (n: NSNotification) in
    guard let user = n.userInfo?["user"] as? User else {
    return
    }
    self?.reload(user)
    })

    View Slide

  31. ͜͏ͳΓ·͢
    // Model
    struct User { … }
    API.fetchUser(id: id) { (user: User) in
    Notifwift.post("DidUserReceivedNotification", payload: user)
    }
    // View
    let notifwift = Notifwift() // property
    notifwift.observe("DidUserReceivedNotification") {
    [weak self] (payload: User) in
    self?.reload(payload)
    }
    QBZMPBEʹ͍ͨ͠ΠϯελϯεΛɺ
    ԿͰ͋Ζ͏ͱμΠϨΫτʹ౉ͤ·͢ɻ
    ड͚औΔ࣌΋؆୯ɾ҆શʂ
    ܕਪଌ͕ޮ͘ͷͰɺ
    QBZMPBEͷܕ͕6TFSͩͱ
    ࣮֬ʹอূͰ͖Δɻ
    ͞Βʹʜ

    View Slide

  32. ࠷ऴతʹ͸͜͏ͳΔ
    // Model
    struct User { … }
    API.fetchUser(id: id) { (user: User) in
    Notifwift.post("DidUserReceivedNotification", payload: user)
    }
    // View
    let notifwift = Notifwift() // property
    notifwift.observe("DidUserReceivedNotification", block: reload)
    6TFS
    7PJEܕͷؔ਺͔ͩΒɺ͜Ε͚ͩͰ0,

    View Slide

  33. /PUJGXJGUΛಋೖ͢Δͱ

    ଞʹ΋͜Μͳ͍͍͜ͱ͕͋ΔΑʂ
    ஋͖ͭ&OVNͱͷ૬ੑ΋ൈ܈
    /PUJGXJGUΠϯελϯε͕ࢮ͵ͱ

    ࣗಈతʹSFNPWF0CTFSWFSͯ͘͠Ε·͢ɻ

    Α͋͘ΔSFNPWF0CTFSWFS๨Ε΋

    ͜ΕͰ҆৺ʂ

    View Slide

  34. ੋඇ࢖ͬͯΈ͍ͯͩ͘͞Ͷʂ
    IUUQTHJUIVCDPNUBLBTFL/PUJGXJGU
    ΋͠
    ؾʹೖͬͨΒ
    (JU)VC
    4UBS΋
    ΑΖ͘͠Ͷʂ

    View Slide

  35. /PUJGXJGUͰ

    /4/PUJGJDBUJPOVTFS*OGPΛ

    4XJGUZʹѻ͓͏
    QPUBUPUJQT
    CZ
    ͓ΘΓ
    <13>

    View Slide