1

I wrote what I thought was a basic function to sum up values of an array and calculate an amount left in budget. I use a for loop to sum the elements of an array, then subtract this value from the budget. However, for some reason the value of sum updates properly in the for loop, but the sum value out of the for loop is always zero. In the below the println "Sum in the loop" is correct, but the println "Sum is" always equals 0. dataModel.spendingDataDisplay is an array of objects. Thanks for any help.

func amountLeftToSpend ()->Double {
        var sum:Double = 0.0
        for spendingItem in dataModel.spendingDataDisplay {
            var sum = spendingItem.amountSpent + sum
            println("Spending Item .amountSpent\(spendingItem.amountSpent)")
            println("Sum in the loop is \(sum)")
        }
        println("Sum is \(sum)")
        let amountLeftInBudget = dataModel.settingsData.weeklyBudget - sum
        println("Amount Left in Budget is \(amountLeftInBudget)")
        return amountLeftInBudget
    }

4 Answers 4

8

As others have pointed out, you have two sum variables. So you could solve this by eliminating the inner var reference:

var sum:Double = 0.0
for spendingItem in dataModel.spendingDataDisplay {
    sum += spendingItem.amountSpent
}

Alternatively, if spendingDataDisplay is a Swift array, you can also use the reduce method:

let sum = dataModel.spendingDataDisplay.reduce(0.0) { $0 + $1.amountSpent }
Sign up to request clarification or add additional context in comments.

Comments

2
var sum:Double = 0.0
    for spendingItem in dataModel.spendingDataDisplay {
        var sum = spendingItem.amountSpent + sum

on the third line you redeclare the variable sum. It should read:

var sum:Double = 0.0
    for spendingItem in dataModel.spendingDataDisplay {
        sum = spendingItem.amountSpent + sum

Im not sure it does actually work either...

xcode playground with example showing it doesn't work

Interestingly we only get the compiler warning outside of its function context

compiler warning

Comments

2

It looks like you're updating the local variable sum inside the for loop, and not the variable declared as a Double outside the loop. That's why the "Sum is" is always 0. I'm not actually sure how the "Sum in the loop" is correct given your code, but I'm not very good with Swift.

I think you need to change var sum = spendingItem.amountSpent + sum

to just sum += spendingItem.amountSpent

Comments

1

You can use reduce function to avoid that kind of errors.

func amountLeftToSpend ()->Double {
    let sum = reduce(dataModel.spendingDataDisplay, 0.0) { $0 + $1.amountSpeed }

    println("Sum is \(sum)")
    let amountLeftInBudget = dataModel.settingsData.weeklyBudget - sum
    println("Amount Left in Budget is \(amountLeftInBudget)")
    return amountLeftInBudget
}

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.