0

我已经阅读了所有堆栈溢出的可能解决方案,但没有一个适用于我。无法识别的选择器NotificationCenter Swift 3

我的代码是

NotificationCenter.default.addObserver(self, selector: #selector(fetchedUser(notification:)) , name: NSNotification.Name.init("dbReady"), object: nil) 

    loggedUser.fetchUserByUID(id: current.uid) 


    return true 
} 

func fetchedUser(notification:NSNotification){ 

    let info = notification.object as! [String : AnyObject] 
    print(info) 

} 

,并在另一个类中我有:

NotificationCenter.default.post(name: NSNotification.Name.init("dbReady"), object: dictionary) 

所有语法选择不工作

我想:

  1. fetchedUser
  2. fetchedUser:
  3. fetchedUser(通知:)
  4. “fetchedUser:”

等10个可能。这个问题让我疯狂!

任何人都可以帮助我吗?

+0

它给你编译器错误? –

+0

[你也读过他的吗?](http://stackoverflow.com/a/40757389/5099208) – Idan

+0

一个独立的示例:http://stackoverflow.com/questions/39351781/swift-2-0-到SWIFT-3-0-nsnotification通知。 –

回答

0

我的错误是,不存在名称为“fetchedUserWithNotification:”的选择器。 我在3小时内重写新课程并复制并粘贴所有内容后解决了我的问题。也许这是一个Xcode错误(恕我直言,最后一个版本很多)

1

在斯威夫特3(系统)通知有这样的标准签名:

func notifFunction(_ notification: Notification) 

所以你的功能应该是

func fetchedUser(_ notification: Notification){ 

和相应的选择是

#selector(fetchedUser(_:)) 

For co nvenience你可以使用的Notification.Name

extension Notification.Name { 
    static let databaseReady = NSNotification.Name("dbReady") 
} 

的扩展,然后你可以写

NotificationCenter.default.addObserver(self, selector: #selector(fetchedUser_:)) , name: .databaseReady, object: nil) 

NotificationCenter.default.post(name: .databaseReady, object: dictionary) 
+1

你确定要强制下划线吗?那么为什么这个http://stackoverflow.com/a/39352319/1187415工作?或者我忽略了什么? –

+0

我不确定,但所有系统通知都有此签名(可能是由于ObjC兼容性) – vadian

0

你可以尝试

NotificationCenter.default.addObserver(self, selector: #selector(YourClassName.fetchedUser), name: notificationName, object: nil) 
0
class Observer { 

    init() { 
     let name = NSNotification.Name("TestNotification") 

     NotificationCenter.default.addObserver(self, 
               selector: #selector(notificationDidTrigger), name: name, object: nil) 
    } 

    @objc func notificationDidTrigger(notification: Notification) { 
     print("Notification triggered ", notification.userInfo) 
    } 
} 


let obj = Observer() 

let name = NSNotification.Name("TestNotification") 
var notification = Notification(name: name, object: nil) 
notification.userInfo = ["Name": "My notification"] 
NotificationCenter.default.post(notification) 
1

它在我的项目中工作。

邮政通知

let dic: [String:AnyObject] = ["news_id": 1 as AnyObject,"language_id" : 2 as AnyObject] 
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "NotificationKeyIndentifier"), object: dic) 

通知观察员

添加以下你想上哪儿去观察所需的类代码。

NotificationCenter.default.addObserver(self, selector: #selector(self.handlePushNotification(notification:)), name: NSNotification.Name(rawValue: "NotificationKeyIndentifier"), object: nil) 

这是通知观察者观察后触发的功能。

func handlePushNotification(notification: NSNotification){ 

    if let dic = notification.object as? [String: AnyObject]{ 

     if let language_id = dic["language_id"] as? Int{ 

      if let news_id = dic["news_id"] as? Int{ 
        print(language_id) 
        print(news_id) 
       } 
      } 
     } 
} 

希望它可以帮助你。如果您对此有任何疑问,请随时填写并提出问题。

相关问题