ios - How to pass a Notification to the method called by the Observer in Swift 3 -


i access notification object sent method below.

var currenttrack:mpmediaitem? {     get{         return playlist?.items[index]     }     set{         print(newvalue?.title!)         //self.index = locateindex(track: newvalue!)         let notif = notification.init(name: playlist.songchangedname, object:self)         notificationcenter.default.post(notif)     } } 

the notifications name defined as:

static let songchangedname = notification.name("songchangednotification") 

here observer:

override init() {     super.init()     notificationcenter.default.addobserver(self,                                            selector: #selector(testselector),                                            name: playlist.songchangedname, //notification.name("songchanged"),                                            object: nil) } 

here method calls:

func testselector(notification:notification){      queuenexttrack()  } 

how pass testselector notification object? know has object parameter of addobserver method.

thank you.

you can rid of problem entirely not using selectors in notifications, timers, etc. there new block based api replace target-selector such

notificationcenter.default.addobserver(forname: playlist.songchangedname, object: nil, queue: nil, using: { notification in     self.testselector(testselector) }) 

for part won't need access notifications in blocks too

func testselector(){     queuenexttrack() }  notificationcenter.default.addobserver(forname: playlist.songchangedname, object: nil, queue: nil) { _ in     self.testselector() } 

or preferred in scenarios:

override init() {     super.init()     let testblock: (notification) -> void = {         self.queuenexttrack()     }     notificationcenter.default.addobserver(forname: playlist.songchangedname, object: nil, queue: nil, using: testblock) } 

edit i'd suggest take @ sample code in description api


Comments