2014-12-05 125 views
0

我有一個類的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

注意:類型名稱應該大寫:'FoodListTable',而不是'foodListTable'。 – 2014-12-06 00:56:52

+0

好的,我會解決這個問題。 – TheCentral 2014-12-06 00:57:45

回答

1

看起來你會希望有和作爲一個計算屬性,這樣它會給你當前的總和,而不是隻計算一次的值。要做到這一點,與更換calorieTotal聲明:

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

與您當前密碼的問題是,一個類的屬性不能在其聲明中訪問其他屬性。

+0

謝謝!我明白。 – TheCentral 2014-12-06 00:56:45

+0

如果你喜歡,你可以使用@handiansom消化並將其減少到'calorieNumberArray.reduce(0,+)':) – valcanaia 2017-06-30 16:20:35

0

1)確保陣列具有元件以在它們之間迭代
2)構造在環與數組長度爲條件,並用循環
一個使用索引
b在數組)迭代的每個元素)添加一個其他值存在於陣列中的一個基本類型變量即後,總

+0

我很抱歉,但我對編程很陌生。我添加了整個班級。我想知道是否可以提供我需要添加/更改的代碼,因爲現在我添加了該類。謝謝。 – TheCentral 2014-12-06 00:29:04

+0

內特庫克給出了代碼。 – 2014-12-09 16:16:19

0
Swift 3. 

let sum = calorieNumberArray.reduce(0, +)

相關問題