我有一个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
}
}
}
答案 0 :(得分:1)
看起来您希望将该总和作为计算属性,以便它将为您提供当前总和,而不是仅计算一次的值。为此,请将calorieTotal
声明替换为:
var calorieTotal: Int {
return calorieNumberArray.reduce(0) { $0 + $1 }
}
您当前代码的问题是类的属性无法访问其声明中的其他属性。
答案 1 :(得分:0)
1)确保数组具有迭代它们的元素
2)构造循环,将数组长度作为条件并使用in循环
a)使用索引
来迭代数组中的每个元素
b)在基本类型变量中添加一个在数组中存在的另一个值之后,即总计
答案 2 :(得分:0)
Swift 3.
让sum = calorieNumberArray.reduce(0,+)