无法将类型 'JSQPhotoMediaItem?' 的值转换为预期的参数类型 'String?'
Cannot convert value of type 'JSQPhotoMediaItem?' to expected argument type 'String?'
我正在尝试在 Xcode (Swift) 中使用 Firebase 实现 JSQMessageViewController。
我收到一个错误:无法将类型 'JSQPhotoMediaItem?' 的值从以下代码转换为预期的参数类型 'String?':
func observeMessages() {
messageRef.observe(.childAdded) { (snapshot) in
//print(snapshot.value)
if let dict = snapshot.value as? [String: AnyObject] {
let mediaType = dict["MediaType"] as! String
let senderId = dict["senderId"] as! String
let senderName = dict["senderName"] as! String
switch mediaType {
case "TEXT":
let text = dict["text"] as! String
self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, text: text))
case "PHOTO":
let fileUrl = dict["fileUrl"] as! String
let url = NSURL(string: fileUrl)
let data = NSData(contentsOf: url! as URL)
let picture = UIImage(data: data! as Data)
let photo = JSQPhotoMediaItem(image: picture)
self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, text: photo))
default:
print("unknown data type")
}
self.collectionView.reloadData()
}
}
}
您应该在 JSQMessage
的初始化程序中将 text
更改为 media
:
self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, media: photo))
仅供参考,JSQMessagesViewController
已 弃用 。 See this blog post for more information.
我正在尝试在 Xcode (Swift) 中使用 Firebase 实现 JSQMessageViewController。
我收到一个错误:无法将类型 'JSQPhotoMediaItem?' 的值从以下代码转换为预期的参数类型 'String?':
func observeMessages() {
messageRef.observe(.childAdded) { (snapshot) in
//print(snapshot.value)
if let dict = snapshot.value as? [String: AnyObject] {
let mediaType = dict["MediaType"] as! String
let senderId = dict["senderId"] as! String
let senderName = dict["senderName"] as! String
switch mediaType {
case "TEXT":
let text = dict["text"] as! String
self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, text: text))
case "PHOTO":
let fileUrl = dict["fileUrl"] as! String
let url = NSURL(string: fileUrl)
let data = NSData(contentsOf: url! as URL)
let picture = UIImage(data: data! as Data)
let photo = JSQPhotoMediaItem(image: picture)
self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, text: photo))
default:
print("unknown data type")
}
self.collectionView.reloadData()
}
}
}
您应该在 JSQMessage
的初始化程序中将 text
更改为 media
:
self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, media: photo))
仅供参考,JSQMessagesViewController
已 弃用 。 See this blog post for more information.