Ios Swift:如何在标签中显示Int?

Ios Swift:如何在标签中显示Int?,ios,swift,uitableview,Ios,Swift,Uitableview,我需要在TableViewCell标签中显示Int以获得值的总和。 这是我的密码: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "eventCell", for: indexPath) as! Budg

我需要在TableViewCell标签中显示Int以获得值的总和。 这是我的密码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "eventCell", for: indexPath) as! BudgetTableViewCell
    
    let budgetEvent: BudgetModel
    budgetEvent = budgetList[indexPath.row]
  
    cell.nameEventLabel.text = budgetEvent.eventName
    cell.spentBudgetLabel.text = String(budgetEvent.spentBudget!)
    
    
    let totalSpent = budgetList.map{ $0.spentBudget! }.reduce(0, +)
    print("sum \(totalSpent)")
    return cell
}

当我运行我的应用程序时,我收到一条错误消息:“线程1:致命错误:在展开可选值时意外发现零”,并且该值为零。

您试图强制展开您的值,这不是一个好的做法,因为如果该值不存在,您的应用程序将失败/崩溃

强制展开意味着您使用运算符,告诉编译器您确定存在一个值,并且我们可以提取它。在以下行中,您将使用“强制展开”:

// 1
cell.spentBudgetLabel.text = String(budgetEvent.spentBudget!)
// 2
let totalSpent = budgetList.map{ $0.spentBudget! }.reduce(0, +)
很难说是哪一个原因导致了您的错误,但您可以改进代码,这将帮助您识别问题:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "eventCell", for: indexPath) as! BudgetTableViewCell

    let budgetEvent = budgetList[indexPath.row]

    cell.nameEventLabel.text = budgetEvent.eventName
    if let spentBudget = budgetEvent.spentBudget {
        cell.spentBudgetLabel.text = String(spentBudget)
    } else {
        print("SpentBudget is empty")
    }

    let totalSpent = budgetList.compactMap{ $0.spentBudget }.reduce(0, +)
    print("sum \(totalSpent)")
    return cell
}

我将
map
函数替换为
compactMap
,它将只返回非可选值。您可以了解这一点

如果您试图强制展开您的值,这不是一种好的做法,因为如果该值不存在,您的应用程序将失败/崩溃

强制展开意味着您使用运算符,告诉编译器您确定存在一个值,并且我们可以提取它。在以下行中,您将使用“强制展开”:

// 1
cell.spentBudgetLabel.text = String(budgetEvent.spentBudget!)
// 2
let totalSpent = budgetList.map{ $0.spentBudget! }.reduce(0, +)
很难说是哪一个原因导致了您的错误,但您可以改进代码,这将帮助您识别问题:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "eventCell", for: indexPath) as! BudgetTableViewCell

    let budgetEvent = budgetList[indexPath.row]

    cell.nameEventLabel.text = budgetEvent.eventName
    if let spentBudget = budgetEvent.spentBudget {
        cell.spentBudgetLabel.text = String(spentBudget)
    } else {
        print("SpentBudget is empty")
    }

    let totalSpent = budgetList.compactMap{ $0.spentBudget }.reduce(0, +)
    print("sum \(totalSpent)")
    return cell
}
我将
map
函数替换为
compactMap
,它将只返回非可选值。你可以读到这个

你可以这样使用

cell.spentBudgetLabel.text=字符串(格式:“%d”,budgetEvent.spentBudget)

您可以这样使用


cell.spentBudgetLabel.text=字符串(格式:“%d”,budgetEvent.spentBudget)

如果您的spentBudget是可选的,则需要提供一个默认值,以防它为零
budgetEvent.spentBudget??0
这是否回答了您的问题?我称之为
强制展开运算符“如果为零则崩溃”运算符。你应该完全避免它,直到你对选项非常非常满意。如果你的spentBudget是可选的,你需要提供一个默认值,以防它是nil
budgetEvent.spentBudget??0
这是否回答了您的问题?我称之为
强制展开运算符“如果为零则崩溃”运算符。你应该完全避免它,直到你真的,真的很舒服的选择。谢谢。这很有帮助。但现在我的标签是空的。我是否需要在此函数中从Firebase获取值?如果是,您可以帮助我如何获取?您应该在另一个函数中获取值,在该函数中下载数据,完成后重新加载tableView。如果你不想看到一个空的tableView,你可以实现一个加载,该加载在下载数据时显示,在下载完成时隐藏。我认为这很好,因为显示了“cell.nameEventLabel.text=budgetEvent.eventName”,但我不知道为什么带有Int值的标签是空的。它正在打印“}否则{打印(“SpentBudget是空的”)”从你写的内容来看,我认为你的“budgetEvent.SpentBudget”是空的,您应该在数据库侧再次检查。谢谢。这很有帮助。但是现在我的标签是空的。我需要在这个函数中从Firebase获取值吗?如果需要,您可以帮助我如何获取吗?您应该在其他函数中获取值,在那里下载数据,完成后重新加载tableView。如果不需要,请n要查看空的tableView,您可以实现一个加载,该加载在下载数据时显示,在下载完成时隐藏。我在函数中有值。我认为这很有效,因为显示了“cell.nameEventLabel.text=budgetEvent.eventName”,但我不知道为什么带有Int值的标签是空的。它正在打印“}否则{print(“SpentBudget是空的”)”,根据您编写的内容,我认为您的“budgettevent.SpentBudget”是空的,您应该在数据库端再次检查它