Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 删除单元格时的字典值-在展开可选值时意外发现nil_Swift_Xcode_Uitableview_Dictionary - Fatal编程技术网

Swift 删除单元格时的字典值-在展开可选值时意外发现nil

Swift 删除单元格时的字典值-在展开可选值时意外发现nil,swift,xcode,uitableview,dictionary,Swift,Xcode,Uitableview,Dictionary,我在Swift/Xcode工作,我是应用程序开发新手,我有一个带有2个标签的tableView。问题是在删除我的tableView的单元格时。我需要获取其中一个单元格标签的数据,但应用程序因致命错误而崩溃—“在展开可选值时意外发现nil” 我已经设置了断点,这样我就可以检查字典中是否存储了任何内容,它确实是这样做的,其中存储了一个元素 谢谢你的帮助 func tableView(tableView: UITableView, commitEditingStyle editingStyle: U

我在Swift/Xcode工作,我是应用程序开发新手,我有一个带有2个标签的tableView。问题是在删除我的tableView的单元格时。我需要获取其中一个单元格标签的数据,但应用程序因致命错误而崩溃—“在展开可选值时意外发现nil”

我已经设置了断点,这样我就可以检查字典中是否存储了任何内容,它确实是这样做的,其中存储了一个元素

谢谢你的帮助

 func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
  if editingStyle == UITableViewCellEditingStyle.Delete {
    let cell = tableView.cellForRowAtIndexPath(indexPath) as! myCustomCell

       self.cost = self.total
      // This line gives me the error** self.total = self.cost - self.dict[cell.cellPrice.text!]!
       self.totalLabel.text = "Total: \(self.total)"

       self.dict.removeValueForKey(cell.cellTitle!.text!)
       tableView.reloadData()
       saveState()



}
这是我设置单元格的代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
   let cell: myCustomCell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! myCustomCell 
   var myDataSource : [String] = Array(self.dict.keys) 
   let key = myDataSource[indexPath.row] as String 
   cell.cellTitle?.text = myDataSource[indexPath.row] 
   cell.cellPrice?.text = "£(self.dict[key]!)"
   self.saveState() 
   return cell 
}

您正在尝试强制展开。我猜是cellPrice标签或文字,但这是其中之一。找出哪一个是nil。

我必须查看您的更多代码才能知道如何设置
myCustomCell
的属性,但它看起来可能是
cellPrice.text
为nil或
self.dict[cell.cellPrice.text]
为nil。你应该安全地打开那些选项,而不是强迫它们。看起来是这样的:

if let price = cell.cellPrice.text, let priceValue = self.dict[price] {
    self.totalLabel.text = "Total: \(priceValue)"
    ...
}

一般来说,您应该远离强制展开(使用!),除非您只是为了测试或其他目的快速编写。这会使调试更加困难。

为什么要将单元格设置为
committeeditingstyle
?从你的数据模型中获取值,而不是从单元格中获取值。我认为@crowerspeak的答案是正确的。问题是强制展开“!”是不安全的。OP可能想知道为什么他在代码中得到这个零。这个答案不会使零消失。它可以阻止碰撞,但不能解决问题对不起,我不好。我更新了我的答案。在这种情况下说debug更容易,因为没有太多可能出现问题的东西。@请说出我的代码来设置单元格:func tableView(tableView:UITableView,cellforrowatinexpath indexath:nsindexath)->UITableViewCell{let cell:myCustomCell=tableView.dequeueReusableCellWithIdentifier(“customCell”,forindexath:indexath)as!myCustomCell var myDataSource:[String]=Array(self.dict.keys)let key=myDataSource[indexath.row]as String cell.cellttitle?.text=myDataSource[indexath.row]cell.cellPrice?.text=“(self.dict[key]!”self.saveState()返回单元格}如果你能将此代码添加到你的问题中,那么我们可以更好地帮助你。我使用了上面的代码来阻止应用程序崩溃,并删除手机,太棒了!但是我需要从“total”文本标签中减去“cellPrice”值,因为每个单元格都有一个数量,并且总计(TotalAbel)显示在tableView下面。好的,这样绘制tableView。单元格的标签是否具有您想要的值,还是为空?