【问题标题】:How to find the sum of all numbers in an array?如何找到数组中所有数字的总和?
【发布时间】:2014-12-05 23:24:21
【问题描述】:

我有一个 UITableViewController 类:

class foodListTable: UITableViewController

我有一个包含 9 个数值的数组:

var calorieNumberArray = [0,0,0,0,0,0,0,0,0]

(是的,数组中的值确实发生了变化。)我试图找到数组中所有值的总和。我已经尝试过创建这个常量:

let calorieTotal = calorieNumberArray.reduce(0) { $0 + $1 }

我使用苹果开发者页面在https://developer.apple.com/documentation/swift/array 解决了这个问题。每当我使用 let calorieTotal = array.reduce(0) { $0 + $1 } 时,我都会收到错误消息:“'foodListTable.Type' 没有名为 'calorieNumberArray' 的成员”

我该如何解决这个问题?我需要更改在数组中添加数字的方式吗?

这是我的这个类的所有代码:

class foodListTable: UITableViewController {
var calorieNumberArray = [0,0,0,0,0,0,0,0,0]
let calorieTotal = calorieNumberArray.reduce(0) { $0 + $1 }
var foods = [Food]()
override func viewDidLoad() {
    super.viewDidLoad()
    self.foods = [Food(Name: "Small French Fries: 197 Cal."),Food(Name: "Cheeseburger: 359 Cal., One Patty"),Food(Name: "Cheese Pizza: 351 Cal., One Slice"),Food(Name: "Fried Chicken Breast: 320 Cal."),Food(Name: "Large Taco: 571 Cal."),Food(Name: "Hotdog: 315 Cal., With Ketchup"),Food(Name: "Tuna Sandwich: 287 Cal."),Food(Name: "1 Cup Vanilla Ice Cream: 290 Cal."),Food(Name: "1 1/2 Cup Vegetable Salad: 30 Cal.")]
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.foods.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    var food : Food
    food = foods[indexPath.row]
    cell.textLabel.text = food.Name
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if let cell = tableView.cellForRowAtIndexPath(indexPath) {
        if cell.accessoryType == .None {

            if indexPath.row == 0 {
                calorieNumberArray[0] = 197
            }
            if indexPath.row == 1 {
                calorieNumberArray[1] = 359
            }
            if indexPath.row == 2 {
                calorieNumberArray[2] = 351
            }
            if indexPath.row == 3 {
                calorieNumberArray[3] = 320
            }
            if indexPath.row == 4 {
                calorieNumberArray[4] = 571
            }
            if indexPath.row == 5 {
                calorieNumberArray[5] = 315
            }
            if indexPath.row == 6 {
                calorieNumberArray[6] = 287
            }
            if indexPath.row == 7 {
                calorieNumberArray[7] = 290
            }
            if indexPath.row == 8 {
                calorieNumberArray[8] = 30
            }

            cell.accessoryType = .Checkmark
        } else {

            if indexPath.row == 0 {
                calorieNumberArray[0] = 0
            }
            if indexPath.row == 1 {
                calorieNumberArray[1] = 0
            }
            if indexPath.row == 2 {
                calorieNumberArray[2] = 0
            }
            if indexPath.row == 3 {
                calorieNumberArray[3] = 0
            }
            if indexPath.row == 4 {
                calorieNumberArray[4] = 0
            }
            if indexPath.row == 5 {
                calorieNumberArray[5] = 0
            }
            if indexPath.row == 6 {
                calorieNumberArray[6] = 0
            }
            if indexPath.row == 7 {
                calorieNumberArray[7] = 0
            }
            if indexPath.row == 8 {
                calorieNumberArray[8] = 0
            }

            cell.accessoryType = .None
        }
    }
}

【问题讨论】:

  • 注意:类型名称应该大写:FoodListTable,而不是foodListTable
  • 好的,我会解决的。

标签: arrays uitableview swift sum


【解决方案1】:

您似乎希望将该总和作为一个计算属性,以便它为您提供当前总和,而不是只计算一次的值。为此,请将calorieTotal 声明替换为:

var calorieTotal: Int { 
    return calorieNumberArray.reduce(0) { $0 + $1 }
}

您当前代码的问题是类的属性无法访问其声明中的其他属性。

【讨论】:

  • 谢谢!我明白了。
  • 如果您愿意,可以使用@handiansom sugestion 并将其缩减为calorieNumberArray.reduce(0, +) :)
【解决方案2】:

1)确保数组有元素可以迭代它们
2) 以数组长度为条件并以 in 循环构造循环
a) 使用索引迭代数组中的每个元素
b)在原始类型变量中的数组中存在的另一个值之后添加一个,即总计

【讨论】:

  • 对不起,我对编程很陌生。我添加了整个班级。我想知道您是否可以提供我需要添加/更改的代码,因为现在我添加了该类。谢谢。
【解决方案3】:
Swift 3.

让 sum = calorieNumberArray.reduce(0, +)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    • 2015-12-17
    • 2020-02-05
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多