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

NSNotification.userInfoを
Swiftyに扱おう

704056da9a4c4e075ad14479beaebab7?s=47 takasek
March 16, 2016

 NSNotification.userInfoを
Swiftyに扱おう

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

704056da9a4c4e075ad14479beaebab7?s=128

takasek

March 16, 2016
Tweet

Transcript

  1. /4/PUJGJDBUJPOVTFS*OGPΛ
 4XJGUZʹѻ͓͏ QPUBUPUJQT CZ

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

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

  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) })
  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͕͋Γ·ͯ͠ɺ
  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Λऔಘͨ͠Βʜ
  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ࢦఆͯ͠ೖΕͯʜ
  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) }) ͍ͦ͒
  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Λ ड͚ͨΒʜ
  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ΛऔΓग़ͯ͠
  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Λߋ৽͠·͢
  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ͱ͔ॻ͖ͨ͘ͳ͍ʜ
  13. গ͠୤ઢ͠·͕͢ʜ 4XJGUͰ͸ ࢀরܕΑΓ஋ܕΛ࢖͓͏ͥʂ ͱΑ͘ݴΘΕ·͢

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

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

  16. class User { let id: Int let name: String init(id:

    Int, name: String) { self.id = id self.name = name } }
  17. struct User { let id: Int let name: String }

  18. None
  19. 

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

    var userInfo: [NSObject : AnyObject]? { get }  VTFS*OGP͸</40CKFDU"OZ0CKFDU> ܕ  "OZ0CKFDU͸DMBTT͔͠ड͚ೖΕͳ͍  ͭ·Γɺ
 TUSVDU΋FOVN΋
 /4/PUJpDBUJPOVTFS*OGPͷ
 த਎ͱͯ͠࢖͑ͳ͍
  21. ΍͸Γ
 6TFSͷTUSVDUԽ͸ ఘΊΔ͔͠ͳ͍ͷ͔ʜ

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

  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!! ˢ͜͜஫໨ʂ
  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ͱͯ͠ѻ͑ΔΑ͏ʹʂ
  25. Α͠ ͜ΕͰ͍͚Δʂ

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

    { self.user = user } }
  27. /4/PUJpDBUJPOͷͩΔ͍ͱ͜BHBJO  /4/PUJpDBUJPO$FOUFSͷ
 هड़͕৑௕  VTFS*OGPͷ%JDUJPOBSZ,FZʹ
 ΦϨΦϨ,FZΛ࢖Θ͟ΔΛಘͳ͍  VTFS*OGPͷ7BMVFͱͯ͠
 "OZ$MBTT͔͠ೖΕΒΕͳ͍

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

  29. /PUJGXJGUͰ
 /4/PUJGJDBUJPOVTFS*OGPΛ
 4XJGUZʹѻ͓͏

  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) })
  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ͩͱ ࣮֬ʹอূͰ͖Δɻ ͞Βʹʜ
  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,
  33. /PUJGXJGUΛಋೖ͢Δͱ
 ଞʹ΋͜Μͳ͍͍͜ͱ͕͋ΔΑʂ  ஋͖ͭ&OVNͱͷ૬ੑ΋ൈ܈  /PUJGXJGUΠϯελϯε͕ࢮ͵ͱ
 ࣗಈతʹSFNPWF0CTFSWFSͯ͘͠Ε·͢ɻ
 Α͋͘ΔSFNPWF0CTFSWFS๨Ε΋
 ͜ΕͰ҆৺ʂ

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

  35. /PUJGXJGUͰ
 /4/PUJGJDBUJPOVTFS*OGPΛ
 4XJGUZʹѻ͓͏ QPUBUPUJQT CZ ͓ΘΓ <13>