使用NotificatinCenter处理用户键入通知时出现问题 [英] Issue while handle user typing notification using notificatincenter
问题描述
我正在使用socket.io创建一个聊天应用程序,每件事都很完美,但是当我处理用户键入通知时,却出现如下错误
I am creating an chat application using socket.io, every things going perfect but when I am handling user typing notification I am getting error like below
错误:
由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'-[SocketChat.ChatViewController handleUserTypingNotification:]:无法识别的选择器已发送到实例 0x7f817653d710
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SocketChat.ChatViewController handleUserTypingNotification:]: unrecognized selector sent to instance 0x7f817653d710
现在,我将向您展示我的代码以实现更好的体验
and now I will show you my code for better expiation
代码:
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获取的方法.我通过波纹管这样的通知中心进行管理
Here I am managing method which I am getting from index.js file and from another viewcontroller. I manage with notification center like bellow
let notificationCenter4 = NotificationCenter.default
notificationCenter4.addObserver(self, selector: Selector(("handleUserTypingNotification")), name:NSNotification.Name(rawValue: "userTypingNotification"), object: nil)
我不明白为什么会出现此错误,它的确切含义是什么.谁能帮我吗?
I don't understand why this error is coming and what does it exactly mean. Can any one please help me?
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()
}
}
}
}
推荐答案
您的选择器签名错误.看起来应该像这样.
Your selector signature is wrong. It should look like this.
let notificationCenter4 = NotificationCenter.default
notificationCenter4.addObserver(self, selector: #selector(handleKeyboardDidShowNotification(notification:)), name:NSNotification.Name(rawValue: "userTypingNotification"), object: nil)
在您的代码中,您正在创建一个 new 实例,而您可能已经有了一个选择器.像这样的东西.
In your code you are creating a new instance, while you probably have a selector already. Something like this.
@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()
}
}
}
这篇关于使用NotificatinCenter处理用户键入通知时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!