我有了消息登錄的用戶的列表已經與他/她選擇的任何用戶有一個tableViewController。單擊單元格時,它應該轉到一個視圖控制器,允許登錄用戶與任何用戶聊天。我已經建立了一個覆蓋賽格瑞:火力IOS聊天應用程序無法正常工作
var userpicuid: String?
var username: String?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let message = messages[indexPath.row]
if message.ReceiverId != self.loggedInUserUid {
var newVariable = message.ReceiverId
if self.userpicuid == newVariable {
let ref = FIRDatabase.database().reference().child("users").child(userpicuid!)
ref.observeSingleEvent(of: .value, with: { (snapshot)
in
if let dictionary = snapshot.value as? [String: AnyObject]{
for post in dictionary {
let messages = post.value as! [String: AnyObject]
for (id, value) in messages {
self.username = messages["username"] as? String
}}}})}} else if message.senderId != self.loggedInUserUid {
let newVariable = message.senderId
if self.userpicuid == newVariable {
let ref = FIRDatabase.database().reference().child("users").child(userpicuid!)
ref.observeSingleEvent(of: .value, with: { (snapshot)
in
if let dictionary = snapshot.value as? [String: AnyObject]{
for post in dictionary {
let messages = post.value as! [String: AnyObject]
for (id, value) in messages {
self.username = messages["username"] as? String
}}}})}
}
performSegue(withIdentifier: "MessageNow", sender: self.userpicuid)
}
override public func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier == "MessageNow", let chatVc = segue.destination as? SendMessageViewController else {
return
}
chatVc.senderId = self.loggedInUser?.uid
chatVc.receiverData = sender as AnyObject
chatVc.senderDisplayName = self.userpicuid
chatVc.username = self.username
}
而在目標視圖控制器,這是設置:
var receiverData: AnyObject?
override func viewDidLoad() {
super.viewDidLoad()
self.senderId = FIRAuth.auth()?.currentUser?.uid
let receiverId = receiverData as! String
let receiverIdFive = String(receiverId.characters.prefix(5))
let senderIdFive = String(senderId.characters.prefix(5))
if (senderIdFive > receiverIdFive)
{
self.convoId = senderIdFive + receiverIdFive
}
else
{
self.convoId = receiverIdFive + senderIdFive
}}
我得到的錯誤無法投類型的值「Lit_Swap.MessageTableViewCell」( 0x1091d0f10)到'NSString'(0x10b153c60)。因爲發件人顯然是一個tableviewcell,我將如何設置發件人,這樣我就可以點擊該單元格並進入聊天控制器。
這將有助於具體瞭解哪條線路導致錯誤。設置一個斷點並手動逐行逐行顯示包含錯誤的行。但是,我懷疑這行* chatVc.receiverData =發件人爲AnyObject *,它將發件人分配爲一個值,與此行不匹配* let receiverId = receiverData as! String * – Jay
@Jay線路導致錯誤是'let receiverId = receiverData as!字符串' – juelizabeth
沒錯。根據我的評論。您不能將* sender *分配給字符串,因爲它是不同的類。 *(覆蓋公共FUNC準備(對於SEGUE:UIStoryboardSegue,發件人:任意){(* – Jay