在服务器端,我使用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)
}
似乎我无法向观察者添加选择器,我认为问题可能在于对象数据类型,但我无法弄清楚为什么......
答案 0 :(得分:0)
找到了答案:
NotificationCenter.default.addObserver(self, selector: #selector(self.userTypingNotification), name: NSNotification.Name(rawValue: "userWasDisconnectedNotification"), object: nil)