gpt4 book ai didi

ios - 线程 1 : EXC_BAD_INSTRUCTION when trying to write a message JSQMessagesViewController

转载 作者:行者123 更新时间:2023-11-30 13:05:30 25 4
gpt4 key购买 nike

See this pic for error

当我尝试写消息时它崩溃了。我似乎无法在这里找到错误。我收到一个 Thread 1: EXC_BAD_INSTRUCTION (code=EXC)_1386_INVOP, subcode=0x0. 我试图找出我的字符串有什么问题。抱歉,我刚刚接触 xcode

完整代码如下

import UIKit
import JSQMessagesViewController
import MobileCoreServices
import AVKit
import FirebaseDatabase

class ChatViewController: JSQMessagesViewController {
var messages = [JSQMessage]()

var messageRef = FIRDatabase.database().reference().child("messages")


override func viewDidLoad() {
super.viewDidLoad()

self.senderId = "1"
self.senderDisplayName = "Ej"
//messageRef.childByAutoId().setValue("first message")
//messageRef.childByAutoId().setValue("2nd")
// messageRef.observeEventType(FIRDataEventType.Value) { (snapshot: FIRDataSnapshot) in
// if let dick = snapshot.value as? NSDictionary {
// print(dick)
// }
// }
observeMessages()


}

func observeMessages(){

messageRef.observeEventType(.ChildAdded, withBlock: { snapshot in
//print(snapshot.value)

if let dick = snapshot.value as? [String: AnyObject]{
//let Mediatype = dick["Mediatype"] as! String
let senderId = dick["senderId"] as! String
let senderName = dick["senderName"] as! String
let text = dick["text"] as! String
self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, text: text))
self.collectionView.reloadData()
}
})
}

//Send Btn
override func didPressSendButton(button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: NSDate!) {

//Messages
//messages.append(JSQMessage(senderId: senderId, displayName: senderDisplayName, text: text))
//self.collectionView.reloadData()
// print(messages)

let newMessage = messageRef.childByAutoId()
let messageData = ["text": text, "senderId": senderId, "senderName": senderDisplayName, "Mediatype": "TEXT"]
newMessage.setValue(messageData)



}
//File btn
override func didPressAccessoryButton(sender: UIButton!) {
// ALERT WHEN PRESSED ACCESSORY
let sheet = UIAlertController(title: "Media Messages", message: "Select A Media", preferredStyle: UIAlertControllerStyle.ActionSheet)
let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (alert:UIAlertAction) in

}

let photoLibrary = UIAlertAction(title: "Photo Library", style: UIAlertActionStyle.Default) { (alert: UIAlertAction) in
self.getMediaFrom(kUTTypeImage)
}

let videoLibrary = UIAlertAction(title: "Video Library", style: UIAlertActionStyle.Default) { (alert: UIAlertAction) in
self.getMediaFrom(kUTTypeMovie)


}

sheet.addAction(photoLibrary)
sheet.addAction(videoLibrary)
sheet.addAction(cancel)
self.presentViewController(sheet, animated: true, completion: nil)



//let imagePicker = UIImagePickerController()
//imagePicker.delegate = self
//self.presentViewController(imagePicker, animated: true, completion: nil)


}

func getMediaFrom(type: CFString) {
print(type)
let mediaPicker = UIImagePickerController()
mediaPicker.delegate = self
mediaPicker.mediaTypes = [type as String]
self.presentViewController(mediaPicker, animated: true, completion: nil)



}


override func collectionView(collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
return messages[indexPath.item]
}

//BUBBLE CHAT
override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {
let bubbleFactory = JSQMessagesBubbleImageFactory()
return bubbleFactory.outgoingMessagesBubbleImageWithColor(.lightGrayColor())
}

override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! {
return nil
}





override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print("number of items \(messages.count)")
return messages.count
}


override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell

return cell
}


override func collectionView(collectionView: JSQMessagesCollectionView!, didTapMessageBubbleAtIndexPath indexPath: NSIndexPath!) {

print("didTappedMessageBubbleAtIndexPath \(indexPath.item)")
let message = messages[indexPath.item]
if message.isMediaMessage {
if let mediaitem = message.media as? JSQVideoMediaItem{
let player = AVPlayer(URL: mediaitem.fileURL)
let playerView = AVPlayerViewController()
playerView.player = player

self.presentViewController(playerView, animated: true, completion: nil)
}

}

}



override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func LogOutTapped(sender: AnyObject) {
print ("User Logged Out")
//Goes on to Chat View




//Main Stroryboard Interface
_ = UIStoryboard(name: "Main", bundle: nil)



//From MainStoryboard instatiate a navigation controller

let LoginVC = storyboard?.instantiateViewControllerWithIdentifier("LoginVC") as! LogInViewController



//Get app delegate

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate




//Navigation Controller as root view controller

appDelegate.window?.rootViewController = LoginVC


}


}


extension ChatViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
print("finish picking")
print(info)
if let picture = info[UIImagePickerControllerOriginalImage] as? UIImage {
let photo = JSQPhotoMediaItem(image: picture)
messages.append(JSQMessage(senderId: senderId, displayName: senderDisplayName, media: photo))
}else if let video = info[UIImagePickerControllerMediaURL] as? NSURL{
let videoitem = JSQVideoMediaItem(fileURL: video, isReadyToPlay: true)
messages.append(JSQMessage(senderId: senderId, displayName: senderDisplayName, media: videoitem))
}

self.dismissViewControllerAnimated(true, completion: nil)
collectionView.reloadData()
}



}

最佳答案

该错误告诉您 dick["text"] 实际上是一个意外的 nil。

尝试这样做:

let text = dick["text"] ?? ""

应该用空字符串替换 nil。

关于ios - 线程 1 : EXC_BAD_INSTRUCTION when trying to write a message JSQMessagesViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39424398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com