【发布时间】:2021-05-26 22:48:18
【问题描述】:
我有这段代码,所以我需要如何将花费预算转换为 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
if let spent = budgetEvent.spentBudget{
cell.spentBudgetLabel.text = spent
}else{
print("spent budget is empty")
}
let totalSpent = budgetList.compactMap{ $0.spentBudget }.reduce(0, +)
self.spentBudget.text = String("$ \(totalSpent)")
print("sum \(totalSpent)")
return cell
}'''
【问题讨论】:
-
@JoakimDanielson 谢谢。第二条评论是正确的。
-
我不建议使用
.reduce(0, +),我建议您使用 for 循环手册,因为您似乎不理解它。如果你还想使用reduce(),我会用compactMap{ Int($0.spendBudget) }替换compactMap{ $0.spentBudget}。 -
@Larme 是的,这很有帮助。谢谢。
标签: ios swift string integer converters