0

I have a CollectionView with items like...

Fruit(name: String, price: Int, imageUrl: String)

The user can select multiple Fruits and then proceed to a "checkout" which is a UITableView with all the fruits selected. The UITableViewCell contains: amountLabel, fruitLabel, priceLabel.

I want to show how many of each fruit and the total price is, in this view.

What is the best way to do this?

What I see now :

Banana - 1$
Orange - 3$
Banana - 1$
Apple - 2$
Apple - 2$
Banana - 1$
Orange - 3$
Orange - 3$

total: 16$

What I want to see:

3x Banana - 3$
2x Apple - 4$
3x Orange - 9$

total: 16$
3
  • 2
    You should wrap your Fruit in an Order struct together with an int counter that you increase when the same fruit is added instead of storing a separate fruit. Then you could also have a calculated property in the Order struct that returns the total amount. The Order objects could be stored in a dictionary with the fruit or the fruit name as key Commented May 28, 2019 at 8:49
  • @JoakimDanielson he wants to do that with this data source. That is his question. Commented May 28, 2019 at 8:49
  • 1
    @RakeshaShastri I posted this as a comment and not as an answer which is quite different so I think I am allowed to present an alternative route for OP to consider. Commented May 28, 2019 at 8:54

2 Answers 2

2

You could group the array and sum up the prices

let grouped = Dictionary(grouping: fruits, by: {$0.name})
var total = 0
for (key, value) in grouped {
    let price = value.reduce(0, {$0 + $1.price})
    print(value.count, key, "\(price)$")
    total += price
}
print("total: \(total)$")
Sign up to request clarification or add additional context in comments.

3 Comments

Ah yes, I knew there was a Swifty alternative to NSCountedSet but I couldn't remember what it was. This is a better answer than mine :)
This was a great answer. Sorry for the late reply but I was trying to populate the cells with the information given. I created a new class FilteredFruits() and a new array [filteredFruits]() and after the print(value.count... I also did filteredFruits.append(Fruit(amount: value.count, name: key, price: price) and then populated the cells with filteredFruits[indexPath.row].name etc. Is this the right way to continue? thanks
If that's what you expect, yes.
0

This is a perfect case for NSCountedSet.

You can see the documentation for NSCountedSet here.

You can create an NSCountedSet like...

let countedSet = NSCountedSet(array: fruits)

and then get info out of it like...

countedSet.objectEnumerator.forEach {
    let count = countedSet.count(for: $0)

    print($0.name, count, count * $0.price)
}

For your table view you can use countedSet.objectEnumerator.allObjects.count for the number of rows.

3 Comments

Can you use this as a data source for a table view?
Ok. Let me rephrase. Can he use this to get the table view to show the result like how he wants easily? (His last part of the question)
I am asking if this would work as the data source for the table view to easily achieve his required result format?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.