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


i have class of uitableviewcontroller:

class foodlisttable: uitableviewcontroller 

i have array 9 number values in it:

var calorienumberarray = [0,0,0,0,0,0,0,0,0] 

(yes, values in array change.) trying find sum of values in array. have tried creating constant:

let calorietotal = calorienumberarray.reduce(0) { $0 + $1 } 

i used apple developer page figure out @ https://developer.apple.com/documentation/swift/array. whenever use let calorietotal = array.reduce(0) { $0 + $1 }, error: "'foodlisttable.type' not have member named 'calorienumberarray'"

how can fix this? need change way i'm adding numbers in array?

here of code 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., 1 patty"),food(name: "cheese pizza: 351 cal., 1 slice"),food(name: "fried chicken breast: 320 cal."),food(name: "large taco: 571 cal."),food(name: "hotdog: 315 cal., 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) 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         }     } } 

it looks you'd want have sum computed property, give current sum, , not value calculated once. that, replace calorietotal declaration with:

var calorietotal: int {      return calorienumberarray.reduce(0) { $0 + $1 } } 

the problem current code properties of class can't access other properties in declarations.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -