TheCentral TheCentral -4 years ago 139
Swift Question

How to find the sum of all numbers in an array?

I have a class of UITableViewController:

class foodListTable: UITableViewController


I have an array with 9 number values in it:

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


(Yes, the values in the array do change.) I am trying to find the sum of all the values in the array. I have tried by creating this constant:

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


I used the apple developer page to figure this out at https://developer.apple.com/library/prerelease/ios/documentation/General/Reference/SwiftStandardLibraryReference/Array.html . Whenever I use
let calorieTotal = array.reduce(0) { $0 + $1 }
, I get an error: "'foodListTable.Type' does not have a member named 'calorieNumberArray'"

How can I fix this? Do I need to change the way I'm adding the numbers in the array? I am new to programming, so please provide as much code as possible. Thank you.

Here is all of my code for this class:

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
}
}
}

Answer Source

It looks like you'd want to have that sum as a computed property, so that it will give you the current sum, and not a value that is only calculated once. To do that, replace the calorieTotal declaration with:

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

The problem with your current code is that properties of a class can't access other properties in their declarations.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download