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

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. Α͋͘Δ࣮૷ // 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) })
  2. Α͋͘Δ࣮૷ // 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͕͋Γ·ͯ͠ɺ
  3. Α͋͘Δ࣮૷ // 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Λऔಘͨ͠Βʜ
  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) }) %JDUJPOBSZʹแΜͰ /PUJpDBUJPOͷVTFS*OGPʹ LFZࢦఆͯ͠ೖΕͯʜ
  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) }) ͍ͦ͒
  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) }) 7JFXͰ /PUJpDBUJPOΛ ड͚ͨΒʜ
  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) }) VTFS*OGP͔Β LFZࢦఆͯ͠ 6TFSΛऔΓग़ͯ͠
  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) }) SFMPBE VTFS Ͱ 7JFXΛߋ৽͠·͢
  9. /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ͱ͔ॻ͖ͨ͘ͳ͍ʜ
  10. class User { let id: Int let name: String init(id:

    Int, name: String) { self.id = id self.name = name } }
  11. 

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

    var userInfo: [NSObject : AnyObject]? { get }  VTFS*OGP͸</40CKFDU"OZ0CKFDU> ܕ  "OZ0CKFDU͸DMBTT͔͠ड͚ೖΕͳ͍  ͭ·Γɺ
 TUSVDU΋FOVN΋
 /4/PUJpDBUJPOVTFS*OGPͷ
 த਎ͱͯ͠࢖͑ͳ͍
  13. 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!! ˢ͜͜஫໨ʂ
  14. 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ͱͯ͠ѻ͑ΔΑ͏ʹʂ
  15. ͜Ε͕ʜ // 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) })
  16. ͜͏ͳΓ·͢ // 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ͩͱ ࣮֬ʹอূͰ͖Δɻ ͞Βʹʜ
  17. ࠷ऴతʹ͸͜͏ͳΔ // 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,