在显示我的tableView时,我发现此错误没有任何指示哪个部分错误。
这里是tableViewCell的一部分,可能是问题来源:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Dequeue cell
let cell: UITableViewCell! = self.clientTable.dequeueReusableCellWithIdentifier("ClientCell", forIndexPath: indexPath)
// Unpack message from Firebase DataSnapshot
let messageSnapshot: FIRDataSnapshot! = self.messages[indexPath.row]
let message = messageSnapshot.value as! Dictionary<String, String>
let date = message[Constants.MessageFields.date] as String!
if let imageUrl = message[Constants.MessageFields.imageUrl] {
if imageUrl.hasPrefix("gs://") {
FIRStorage.storage().referenceForURL(imageUrl).dataWithMaxSize(INT64_MAX){ (data, error) in
if let error = error {
print("Error downloading: \(error)")
return
}
cell.imageView?.image = UIImage.init(data: data!)
}
} else if let url = NSURL(string:imageUrl), data = NSData(contentsOfURL: url) {
cell.imageView?.image = UIImage.init(data: data)
}
cell!.textLabel?.text = "sent by: \(date)"
} else {
let text = message[Constants.MessageFields.weight] as String!
cell!.textLabel?.text = date + ": " + text
cell!.imageView?.image = UIImage(named: "ic_account_circle")
if let photoUrl = message[Constants.MessageFields.photoUrl], url = NSURL(string:photoUrl), data = NSData(contentsOfURL: url) {
cell!.imageView?.image = UIImage(data: data)
}
}
return cell!
}
整个项目基于Firebase教程,我只是做了一些测试更改。
这是项目: Github Link
提前致谢。
答案 0 :(得分:2)
我认为这里的问题是你尝试将messageSnapshot.value强制转换为Dictionary,但它失败了。
我建议你换行:
let message = messageSnapshot.value as! Dictionary<String, String>
到
if let message = messageSnapshot.value as? NSDictionary{
}
或
if let message = messageSnapshot.value as? [NSObject: AnyObject]{
}
并修复由更改引起的编译警告。
你仍然会发现崩溃......你的代码中还有其他一些错误......
更重要的是,你应该使用“if let”,“guard let”,可选链接而不是使用“as!”,“as String!”。
这些声明是Apple推荐的。它更安全。它可以帮助您更快地找到您的错误。它会导致更少的崩溃。
答案 1 :(得分:1)
我在你的代码中做的微小改动允许它工作,我改变了几个(imageUrl)到(imageUrl as!String)以保持编译器满意。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Dequeue cell
let cell: UITableViewCell! = self.clientTable.dequeueReusableCellWithIdentifier("ClientCell", forIndexPath: indexPath)
// Unpack message from Firebase DataSnapshot
// changed vvv
//print statements are there just to check the data...
let messageSnapshot: FIRDataSnapshot! = self.messages[indexPath.row]
//print(messageSnapshot)
let message = messageSnapshot.value as! NSDictionary
//print(message)
let date = message.objectForKey("date") as! String
//print(date)
// changed ^^^
//old code
//let message = messageSnapshot.value as! Dictionary<String, String>
//let date = message[Constants.MessageFields.date] as String!
//
if let imageUrl = message[Constants.MessageFields.imageUrl] {
if imageUrl.hasPrefix("gs://") {
FIRStorage.storage().referenceForURL(imageUrl as! String).dataWithMaxSize(INT64_MAX){ (data, error) in
if let error = error {
print("Error downloading: \(error)")
return
}
cell.imageView?.image = UIImage.init(data: data!)
}
} else if let url = NSURL(string:imageUrl as! String), data = NSData(contentsOfURL: url) {
cell.imageView?.image = UIImage.init(data: data)
}
cell!.textLabel?.text = "sent by: \(date)"
} else {
//let text = message[Constants.MessageFields.weight] as! String! <- doesn't work
// changed vvv
let aNum = message.objectForKey("weight") as! NSNumber
let text = aNum.stringValue
// changed ^^^
cell!.textLabel?.text = date + ": " + text
cell!.imageView?.image = UIImage(named: "ic_account_circle")
if let photoUrl = message[Constants.MessageFields.photoUrl], url = NSURL(string:photoUrl as! String), data = NSData(contentsOfURL: url) {
cell!.imageView?.image = UIImage(data: data)
}
}
return cell!
}