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



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

错误:

由于未捕获的异常而终止应用 'NSInvalidArgumentException', reason: '-[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])}
}

在这里,我正在管理从索引.js文件和另一个视图控制器获取的方法。我使用像波纹管这样的通知中心进行管理

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)

在您的代码中,您正在创建一个实例,而您可能已经有一个选择器。像这样的东西。

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

最新更新