我将尽可能清楚地了解这个问题,但如果您需要更多信息,请询问。我有一个tableviewcontroller,其中包含登录用户与该应用程序的其他用户共享的所有消息的列表。当点击登录用户点击一个单元格时,我希望用户能够转到一个视图控制器,允许他们与他们喜欢的任何用户聊天。这个聊天是使用JSQMessageController获得的。但是,当我在tableviewcontroller中设置segue时,显示如下:
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
}}
我在let 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 :(得分:0)
您的sender
是发起segue的UITableViewCell
。将其强制转换为NSString
时崩溃。删除此行
let receiverId = receiverData as! String
在prepareForSegue
执行此操作
chatVc.receiverData = self.userpicuid
答案 1 :(得分:0)
发件人是谁?并且你将它转换为AnyObject。 AnyObject引用类类型,并在您尝试将其强制转换为Swift值类型(String)时进行断言。
请改为尝试:
chatVc.receiverData = NSString(string: sender as! String)