2017-05-30 37 views
0

我會盡量清楚,因爲我可以在這個問題上,但如果你需要更多的信息,請請問。我有一個tableviewcontroller,其中包含登錄用戶與該應用的其他用戶擁有的所有消息的列表。點擊登錄用戶點擊一個單元格時,我希望用戶能夠繼續使用視圖控制器,以便與任何他們喜歡的用戶聊天。這個聊天是使用JSQMessageController獲得的。然而,當我將它設置了SEGUE在tableviewcontroller,下面顯示:塞格使用tableViewCells

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 

    } 

我在MessageViewController得到一個錯誤:

var receiverData: AnyObject? 

    override func viewDidLoad() { 
     super.viewDidLoad() 

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 
     }} 

我上讓receiverId = receiverData的錯誤!字符串: 無法將類型'Chat_App.MessageTableViewCell'(0x10eb2ef10)的值轉換爲'NSString'(0x110ab1c60)。

在不同的視圖控制器

,我有:

@IBAction func sendMessage(_ sender: Any) { 

    performSegue(withIdentifier: "sendMessageToUser", sender: self.userpicuid) 
} 

override public func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    guard segue.identifier == "sendMessageToUser", let chatVc = segue.destination as? SendMessageViewController else { 
     return 
    } 

    chatVc.senderId = self.loggedInUser?.uid 
    chatVc.receiverData = sender as! String! 
    chatVc.senderDisplayName = self.userpicuid 
    chatVc.username = self.username 

} 

而且它完美塞格斯。

+0

你怎麼申報'receiverData'? –

+0

@f_qi var receiverData:AnyObject? – juelizabeth

+0

Sender是一個'UITableViewCell'。你不能將它轉換爲'NSString'。 – nighttalker

回答

0

您的senderUITableViewCell,啓動segue。將它投射到NSString時,您會崩潰。刪除此行

let receiverId = receiverData as! String 

prepareForSegue做到這一點,而不是

chatVc.receiverData = self.userpicuid 
+0

這個問題中更新了它,現在我得到了一個致命錯誤:意外地發現了零,同時在同一行上展開一個可選值 – juelizabeth

+0

在哪一行? –

+0

讓receiverId = receiverData as!字符串 – juelizabeth

0

發件人是任何?並將其轉換爲AnyObject。 AnyObject引用一個類的類型,當你嘗試將它轉換爲Swift值類型(String)時,它會斷言。

試試這個:

chatVc.receiverData = NSString(string: sender as! String)