我是swift:D的新手,并使用JSQMessagesViewController开发了一个简单的聊天应用程序。我阅读并遵循JSQMessagesViewController示例项目(https://www.syncano.io/blog/create-ios-chat-app-part1/),但我的代码出现错误。
class ChatRoomViewController: JSQMessagesViewController {
let incomingBubble = JSQMessagesBubbleImageFactory().incomingMessagesBubbleImageWithColor(UIColor(red: 10/255, green: 180/255, blue: 230/255, alpha: 1.0))
let outgoingBubble = JSQMessagesBubbleImageFactory().outgoingMessagesBubbleImageWithColor(UIColor.lightGrayColor())
var messages = [JSQMessage]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.setup()
self.addDemoMessages()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func reloadMessagesView() {
self.collectionView?.reloadData()
}
func gotoReserve() {
let nextViewController = ReserveViewController(nibName: "ReserveViewController", bundle: nil)
self.navigationController!.pushViewController(nextViewController, animated: true)
}
}
//MARK - Setup
extension ChatRoomViewController {
func addDemoMessages() {
let message = JSQMessage(senderId: "zzz", displayName: "aaa", text: "asasd")
self.messages.append(message)
self.reloadMessagesView()
}
func setup() {
self.senderId = "user"
self.senderDisplayName = "user"
}
}
//MARK - Data Source
extension ChatRoomViewController {
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.messages.count
}
override func collectionView(collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
let data = self.messages[indexPath.row]
return data
}
override func collectionView(collectionView: JSQMessagesCollectionView!, didDeleteMessageAtIndexPath indexPath: NSIndexPath!) {
self.messages.removeAtIndex(indexPath.row)
}
override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {
let data = messages[indexPath.row]
switch(data.senderId) {
case self.senderId:
return self.outgoingBubble
default:
return self.incomingBubble
}
}
override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! {
return nil
}
}
//MARK - Toolbar
extension ChatRoomViewController {
override func didPressSendButton(button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: NSDate!) {
let message = JSQMessage(senderId: senderId, senderDisplayName: senderDisplayName, date: date, text: text)
self.messages += [message]
self.finishSendingMessage()
}
override func didPressAccessoryButton(sender: UIButton!) {
}
}
当我运行它时
错误消息说
***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[JSQMessage messageHash]:无法识别的选择器已发送至实例0x7fdf0a6e1f60'
并且该应用程序终止。
但擦除后运行良好
self.addDemoMessages()
viewDidLoad()中的代码。所以我想这段代码会发生错误,但是我不知道该如何解决:(我该如何解决?
最佳答案
当我将JSQMessage
对象子类化时,也遇到了类似的问题。如果这样做,您只需要确保为消息数据实现has即可。我只需要将此方法添加到我的message
对象中
func messageHash() -> UInt {
return UInt(self.hash)
}
希望有帮助👻
关于ios - 使用JSQMessagesViewController的NSInvalidArgumentException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36370723/