How can I extract and calculate the price for an array of objects which have the properties price


How can I extract and calculate the price for an array of objects which have the properties price



I have an array of objects and each object have the property Price.
What I want is to extract the price for each object and after to make the sum of that prices and at the end to show the TotalPrice in a label.
For the moment my array of prices is going crazy and I don't know why.



This is my code for the moment:


class CartViewController: UIViewController {
var productsInCartArray = [Product]()
var productPricesArray = [Float]()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = cartTableView.dequeueReusableCell(withIdentifier: Constants.identifierCartTotalPriceCell, for: indexPath) as! CartTableViewCell

var totalSum: Float = 0

for eachProduct in productsInCartArray{
productPricesArray.append(eachProduct.price)
totalSum = productPricesArray.reduce(0, +)

cell.cartTotalPriceLabel.text = String(totalSum)

return cell
}
}
}



And here is a screenshot:



enter image description here





Can you post valid code please. You have mismatched curly braces. And please indent your code so it's readable.
– rmaddy
Jun 30 at 2:13





Ok I added the curly braces and the line for let cell, Thanks !
– Florentin Lupascu
Jun 30 at 2:22


let cell





What happens when you click on add to cart button? Can you add the code for that? Also how and when are you adding products to productsInCart array?
– hardik parmar
Jun 30 at 2:49



productsInCart





This is still missing a closing curly brace. If you want effective help format the code correctly... help us help you.
– Steve
Jun 30 at 2:52





@hardikparmar the add function is working. Is adding the correct item from first view to second view. I just need help with my Price Calculation. If you want to know how I append from FirstView to SecondView then here is the code: let products = ((self.tabBarController?.viewControllers![0] as! UINavigationController).topViewController as! ProductsViewController).selectedProductsArray
– Florentin Lupascu
Jun 30 at 11:38



let products = ((self.tabBarController?.viewControllers![0] as! UINavigationController).topViewController as! ProductsViewController).selectedProductsArray




2 Answers
2



productPricesArray is an instance variable, not a local variable, so you're adding an element to productPricesArray each time you generate a cell; if you have two cells, you'll have 4 prices in that array. In addition, each time you reloadData() on this table, it's adding more prices to that array.



You should pull that logic out of this method and instead have it fire before calling reloadData on the table (or some other load time).



The problem is with the productPricesArray.append(eachProduct.price) part in cellForRow.
Well, cellForRow will be called multiple times. You should not put such logics in cellForRow, it is a bad idea.


productPricesArray.append(eachProduct.price)


cellForRow


cellForRow


cellForRow



What you should do is, calculate the price before you call reloadData. For example, where you are adding the product in productsInCart array, you should also add the product price in productPrices array. So in cellForRow, you will only be summing up the array which is fine.


reloadData


productsInCart


productPrices


cellForRow






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV