gpt4 book ai didi

swift - JSQMessageViewController : fatal error: Index out of range

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

我正在使用 JSQMessageViewController 进行聊天实现。我能够通过 json Post 和 get 发送和接收消息。

我能够加载我的聊天记录,没有任何错误。使用计时器(5秒)来检索消息。

它在一段时间内工作正常,每 5 秒重新加载一次消息。

过了一会儿,我突然在下面一行收到“ fatal error :索引超出范围”。

override func collectionView(_ collectionView: JSQMessagesCollectionView, messageDataForItemAt indexPath: IndexPath) -> JSQMessageData {

//app Crashes here : fatal error: Index out of range
return messages[indexPath.item]


}

下面是代码 var messages = JSQMessage

var outgoingBubbleImageView:JSQMessagesBubbleImage! //= JSQMessagesBubbleImageFactory().outgoingMessagesBubbleImage(with: UIColor.jsq_messageBubbleGreen())
var incomingBubbleImageView:JSQMessagesBubbleImage! //= JSQMessagesBubbleImageFactory().incomingMessagesBubbleImage(with: UIColor.jsq_messageBubbleLightGray())


var batchMessages = true

// Refresh Messages
var timer : Timer!

// Don't double load!
var isLoading = false

override func viewDidLoad() {
super.viewDidLoad()

collectionView!.collectionViewLayout.incomingAvatarViewSize = CGSize.zero
collectionView!.collectionViewLayout.outgoingAvatarViewSize = CGSize.zero

outgoingBubbleImageView = JSQMessagesBubbleImageFactory().outgoingMessagesBubbleImage(with: uicolorFromHex(0xDCF8C7))
incomingBubbleImageView = JSQMessagesBubbleImageFactory().incomingMessagesBubbleImage(with: UIColor.jsq_messageBubbleLightGray()) //uicolorFromHex(0xF6F6F6)


let defaults = UserDefaults.standard
let usertype = defaults.object(forKey: "UserType") as! String

self.userType = usertype

//self.getChatHistory(ipAddress+"/VIOService.svc/JSONService/chatorderhistoryget/"+EmailId+"/"+toEmail+"/"+OrderId)//self.finishReceivingMessage()

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Close", style: .plain, target: self, action: #selector(addTapped))
self.sender = userType

self.inputToolbar.contentView?.leftBarButtonItem = nil


// This is a beta feature that mostly works but to make things more stable it is diabled.
collectionView?.collectionViewLayout.springinessEnabled = false

automaticallyScrollsToMostRecentMessage = true

// self.collectionView?.reloadData()
self.collectionView?.layoutIfNeeded()




self.isLoading = false

self.loadMessages()


}


override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

collectionView!.collectionViewLayout.springinessEnabled = false


timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(ChatMessagesViewController.loadMessages), userInfo: nil, repeats: true)


}

func addMessage(_ id: String, text: String) {
print("add message: \(text)")



let message = JSQMessage(senderId: id, displayName: "Me", text: text)


messages.append(message)



}

func loadMessages() {

// self.messages = [JSQMessage]()

// self.messages.removeAll()

if self.isLoading == false {

isLoading = true

self.getChatHistory(mYURL)


finishReceivingMessage()


}

}

func getChatHistory(_ url:String) {

messages.removeAll()
// messages.removeAll(keepingCapacity: true)

let requestURL: URL = URL(string: url)!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL)
let session = URLSession.shared
let task = session.dataTask(with: urlRequest as URLRequest) {data,response,error in

let httpResponse = response as! HTTPURLResponse
let statusCode = httpResponse.statusCode

if (statusCode == 200) {
print("everything is fine, file downloaded successfully.")

print(response!)


do{



let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments)

if let items = json as? [[String: AnyObject]] {



for item in items {

if let sentBy = item["SentBy"] as? String {

if let SentTo = item["SentTo"] as? String {

if let Message = item["Msg"] as? String {

if let IsType = item["IsType"] as? String {

if let mesSentOn = item["MsgSenton"] as? String //Date {

{

//
让添加= chatHistory(SentBy:sentBy,SentTo:SentTo,消息:Message,IsType:IsType,SentOn:mesSentOn)

                                            self.chatHist.append(add)

self.from = SentTo
self.to = sentBy

var message:String=""

if let range = Message.range(of: ";") {

message = Message.substring(from: range.upperBound)
}

self.addMessage(IsType, text: message)


}
}
}
}

}

self.do_collectionView_refresh()

}



}



}
catch {

print("Error with Json: \(error)")
}

}
}


isLoading = false

task.resume()



}

函数 do_collectionView_refresh() {

    DispatchQueue.main.async {

self.collectionView?.reloadData()

}
}


override func senderId() -> String {
return userType
}

override func senderDisplayName() -> String {
return "Me"
}

override func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {

if messages.count > 0 {

return messages.count
}
else {

return 0
}


// return messages.count
}


override func collectionView(_ collectionView: JSQMessagesCollectionView, messageDataForItemAt indexPath: IndexPath) -> JSQMessageData {

//app Crashes here : fatal error: Index out of range
return messages[indexPath.item]


}


override func collectionView(_ collectionView: JSQMessagesCollectionView, messageBubbleImageDataForItemAt indexPath: IndexPath) -> JSQMessageBubbleImageDataSource {

// return messages[indexPath.item].senderId == self.senderId() ? outgoingBubble : incomingBubble

let message = messages[indexPath.item]

// let message = chatHist[indexPath.item]
// 1
if message.senderId == userType { //

return outgoingBubbleImageView
} else { // 3
return incomingBubbleImageView
}

}

我遇到了这个错误,请有人帮忙。

最佳答案

请告诉我这是否有影响:

 override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return messages.count

}

override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageDataForItemAt indexPath: IndexPath!) -> JSQMessageData! {

let data = self.messages[indexPath.row]
return data

}

override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAt indexPath: IndexPath!) -> JSQMessageBubbleImageDataSource! {

let data = messages[indexPath.row]
switch(data.senderId) {
case self.senderId:
return self.outgoingBubbleImageView
default:
return self.incomingBubbleImageView
}
}

关于swift - JSQMessageViewController : fatal error: Index out of range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47162179/

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