2016-09-12 113 views
0

我發現這個錯誤,沒有任何指示,當顯示我的tableView時出現錯誤。EXC_BREAKPOINT(code = EXC_i386_BPT,subcode = 0x0)(lldb)

Screenshot

這裏的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! 

}

整個項目是基於火力地堡的教程,我只是做一個測試的變化很少。

下面是該項目: Github Link

在此先感謝。

回答

1

我在代碼中做的小修改允許它工作,並且我將幾個(imageUrl)更改爲(imageUrl as!字符串)以保持編譯器的快樂。

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! 
    } 
+0

啊,它工作!謝謝 !!我對Firebase相關功能非常陌生,所以不知道如何處理它們。我一定會在你的代碼上研究更多的基礎。再次感謝! – Kei

1

我認爲這裏的問題是您嘗試將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]{ 
} 

並修復編譯變化引起的警告。

而且你仍然會發現崩潰...有你的代碼中的一些其他錯誤...

更重要的是,你應該使用「如果讓」,「後衛讓利」,可選的鏈接,而不是使用「 as!「,」as String!「。

這些陳述是Apple推薦的。它更安全。它可以幫助你更快找到你的錯誤。它會導致更少的崩潰。

+0

你說得對!我只是從以前的代碼進行修改,所以出現了粗心的錯誤...感謝您的建議! – Kei

相關問題