Share
Embed
NSNotification.userInfoを Swiftyに扱おう
potatotips #27 (http://connpass.com/event/27156/) の発表資料です。https://github.com/takasek/Notifwift の紹介です。
takasek
March 16, 2016
Transcript
35 slides
-
Α͋͘Δ࣮ // 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) }) -
Α͋͘Δ࣮ // 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͕͋Γ·ͯ͠ɺ -
Α͋͘Δ࣮ // 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Λऔಘͨ͠Βʜ -
Α͋͘Δ࣮ // 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ࢦఆͯ͠ೖΕͯʜ -
Α͋͘Δ࣮ // 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) }) ͍ͦ͒ -
Α͋͘Δ࣮ // 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Λ ड͚ͨΒʜ -
Α͋͘Δ࣮ // 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ΛऔΓग़ͯ͠ -
Α͋͘Δ࣮ // 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Λߋ৽͠·͢ -
/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ͱ͔ॻ͖ͨ͘ͳ͍ʜ -
class User { let id: Int let name: String init(id:
Int, name: String) { self.id = id self.name = name } } -
public class NSNotification : NSObject, NSCopying, NSCoding { … public
var userInfo: [NSObject : AnyObject]? { get } VTFS*OGP</40CKFDU"OZ0CKFDU> ܕ "OZ0CKFDUDMBTT͔͠ड͚ೖΕͳ͍ ͭ·Γɺ TUSVDUFOVN /4/PUJpDBUJPOVTFS*OGPͷ தͱͯ͑͠ͳ͍ -
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!! ˢ͜͜ʂ -
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ͱͯ͠ѻ͑ΔΑ͏ʹʂ -
͍͚Δʜ͚Ͳ Πέͯͳ͍ʜ class UserContainer { let user: User init(user: User)
{ self.user = user } } -
͜Ε͕ʜ // 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) }) -
͜͏ͳΓ·͢ // 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ͩͱ ࣮֬ʹอূͰ͖Δɻ ͞Βʹʜ -
࠷ऴతʹ͜͏ͳΔ // 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,