我正在使用socket.io创建一个聊天应用程序,每件事都很完美,但是当我在处理用户键入通知时,出现如下错误
错误:
由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'-[SocketChat.ChatViewController handleUserTypingNotification:]:无法识别的选择器已发送到实例 0x7f817653d710
现在,我将向您展示我的代码以实现更好的体验
代码:
private func listenForOtherMessages() {
socket.on("userTypingUpdate") { (dataArray, socketAck) -> Void in
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "userTypingNotification"), object: dataArray[0] as? [String: AnyObject])}
}
在这里,我正在管理从index.js文件和另一个ViewController获取的方法。我通过波纹管这样的通知中心进行管理
let notificationCenter4 = NotificationCenter.default
notificationCenter4.addObserver(self, selector: Selector(("handleUserTypingNotification")), name:NSNotification.Name(rawValue: "userTypingNotification"), object: nil)
我不明白为什么会出现此错误,它的确切含义是什么。谁能帮我吗?
import UIKit
class ChatViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let notificationCenter4 = NotificationCenter.default
notificationCenter4.addObserver(self, selector: #selector(handleUserTypingNotification)), name:NSNotification.Name(rawValue: "userTypingNotification"), object: nil)
}
func handleKeyboardDidShowNotification(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
conBottomEditor.constant = keyboardFrame.size.height
view.layoutIfNeeded()
}
}
}
}
答案 0 :(得分:0)
您的选择器签名错误。看起来应该像这样。
let notificationCenter4 = NotificationCenter.default
notificationCenter4.addObserver(self, selector: #selector(handleKeyboardDidShowNotification(notification:)), name:NSNotification.Name(rawValue: "userTypingNotification"), object: nil)
在您的代码中,您正在创建一个 new 实例,而您可能已经有了一个选择器。像这样的东西。
@objc func handleKeyboardDidShowNotification(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
conBottomEditor.constant = keyboardFrame.size.height
view.layoutIfNeeded()
}
}
}