1

So I have a struct of Dice that has a few properties attached to it. I want to be able to add them all together so I have one clean value. Here is the struct:

struct Dice: Identifiable, Hashable {
    var id = UUID()
    var displayValue: String
    var initialValue: Int
    var endingValue: Int
            
    mutating func roll() {
        let randomInt = Int.random(in: initialValue..<endingValue)
        displayValue = "\(randomInt)"
        print("Initial: \(initialValue), EndingValue: \(endingValue), Display: \(displayValue)")
    }
}

They are stored within an array here: @State var viewArray: [Dice] = [] and then displayed in an ForEach here:

ForEach(0..<viewArray.count, id: \.self) { index in
    DiceView(dice: viewArray[index])
    .onTapGesture {
        self.viewArray.remove(at: index)
        withAnimation(.spring()) {
          
        }
    }
}

The thing I'm trying to do is grab the displayValue of each item in the viewArray and add them together. What is the best way of doing so? I'm assuming I need to create some sort of array based on the property of displayValue and then add that array together, but I haven't come across it yet.

3
  • So you want to create one string out of all the displayValue strings in the array, I would suggest map to get the property into an array and then joined to make one string from the array Commented Aug 8, 2020 at 18:20
  • Whoops! That's my fault, the displayValue should actually be an Int but essentially yes. How would I use map to grab the displayValue property directly? The examples I see are for grabbing just the direct values, but since my array is a custom type how do I map the custom values? Commented Aug 8, 2020 at 18:23
  • Not sure I follow, the main usage of map is to get a single property. Maybe you should read up on using map. Commented Aug 8, 2020 at 18:27

2 Answers 2

1

If I understood you correctly you can try map + reduce.

Assuming the displayValue is of type Int (as you mentioned in the comments):

var viewArray: [Dice] = ...
let sum = viewArray.map(\.displayValue).reduce(0, +)

Assuming the displayValue is of type String you need to convert it to Int first:

var viewArray: [Dice] = ...
let sum = viewArray.map(\.displayValue).compactMap(Int.init).reduce(0, +)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I think I ended up solving it but a little be more verbose: let rollValues = viewArray.compactMap { Int($0.displayValue) } let total = rollValues.reduce(0,+) print("\(total)") Same principle but different method I think. Thank you!
0

Per @joakim I ended up solving this by creating:

 let rollValues = viewArray.compactMap { Int($0.displayValue) }
    let total = rollValues.reduce(0,+)
    print("\(total)")

and then assigning that to the variable I needed to display. Worked like a charm!

Comments

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.