使用 notificatincenter 处理用户键入通知时出现问题

Issue while handle user typing notification using notificatincenter

我正在使用 socket.io 创建一个聊天应用程序,一切都很完美,但是当我处理用户输入通知时,出现如下错误

错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SocketChat.ChatViewController handleUserTypingNotification:]: unrecognized selector sent to instance 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()
            }
        }
    }

}

您的选择器签名有误。它应该是这样的。

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()
        }
    }
}