【问题标题】:NotificationCenter throw error when adding observer with selector function使用选择器功能添加观察者时,NotificationCenter 抛出错误
【发布时间】:2017-07-13 21:55:19
【问题描述】:

在服务器端,我使用 socket.io 库来发送一些 JS 对象数据类型,在用户输入他们的设备时:

var typingUsers = {};

    clientSocket.on("startType", function(clientNickname){
        typingUsers[clientNickname] = 1;
        io.emit("userTypingUpdate", typingUsers);
    });

当应用收到数据时,会发布通知:

 private func listenForOtherMessages() {
     socket.on("userTypingUpdate") { (dataArray, socketAck) -> Void in
     NotificationCenter.default.post(name:NSNotification.Name(rawValue: "userTypingNotification"), object: dataArray[0] as! [String: AnyObject])
       }
  }

然后将通知作为观察者添加到视图控制器中:

NotificationCenter.default.addObserver(self, selector: Selector(("handleDisconnectedUserUpdateNotification:")), name: NSNotification.Name(rawValue: "userWasDisconnectedNotification"), object: nil) 

这个实现总是抛出一个错误“无法识别的选择器发送到...”

但是没有选择器的以下实现可以正常工作:

NotificationCenter.default.addObserver(forName:NSNotification.Name(rawValue: "userTypingNotification"), object: nil, queue: nil){ object in
             print(object)

}

似乎我无法向观察者添加选择器,我认为问题可能出在对象数据类型上,但我无法真正弄清楚原因..

【问题讨论】:

    标签: ios socket.io nsnotificationcenter


    【解决方案1】:

    找到答案:

    NotificationCenter.default.addObserver(self, selector: #selector(self.userTypingNotification), name: NSNotification.Name(rawValue: "userWasDisconnectedNotification"), object: nil)
    

    older post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      • 1970-01-01
      • 2017-06-16
      • 1970-01-01
      • 2018-01-10
      • 1970-01-01
      相关资源
      最近更新 更多