Home > Software engineering >  Variable not retaining its value
Variable not retaining its value

Time:09-24

Hi I'm new to swift so bear with me if I'm missing something obvious.

I have a function bestSum which accepts 2 parameters targetNumber and numbers, which is an array. The bestSum function returns an array which contains elements from numbers which can be added to get the targetNumber. The returned array is also the shortest combination of numbers that adds up to form targetNumber . If no combination of numbers could add up to targetNumber then the function bestSum returns nil.

    var bestSumMemoryDictionary : [Int : [Int]?] = [:]

    print(bestSum(targetNumber: 4, numbers: [2,4], memoryDictionary: &bestSumMemoryDictionary))



 func bestSum(targetNumber : Int , numbers : [Int] , memoryDictionary : inout [Int : [Int]? ] ) -> [Int]? {
    
    if let value = memoryDictionary[targetNumber]{
        return value
    }
    
    if targetNumber == 0 {
        return []
    }
    
    if targetNumber < 0 {
        return nil
    }
    
    var returningArray : [Int]? = nil
    
    for number in numbers {
        
        let remainder = targetNumber - number
        
        if var bestSumValueArray =  bestSum(targetNumber: remainder, numbers: numbers, memoryDictionary: &memoryDictionary){
            
            bestSumValueArray.append(number)
            
            guard var returningArray = returningArray else {
                returningArray = bestSumValueArray
                continue
            }
       
            if bestSumValueArray.count < returningArray.count{
                returningArray = bestSumValueArray
            }
            print(returningArray)
        }
        print(returningArray)
        
    }
    
    memoryDictionary[targetNumber] = returningArray
    return returningArray
}

The returned array is Optional([2, 2]) to generate the sum of 4 , where as the expected and correct value is Optional([4]).

Issue is happening in the line that contains returningArray = bestSumValueArray . The print statement below that line is printing [4] , Where as the next print statement print Optional([2, 2]) . Why the value of returningArray is different in 2 consecutive print statements ? What am i missing here. Would appreciate any help.

Debugger ScreenShot

CodePudding user response:

In your code you define 2 variables with same name :

var returningArray : [Int]? = nil // 1st definition

for number in numbers {
    
    let remainder = targetNumber - number
    
    if var bestSumValueArray =  bestSum(targetNumber: remainder, numbers: numbers, memoryDictionary: &memoryDictionary){
        
        bestSumValueArray.append(number)
        
        guard var returningArray = returningArray else {
            returningArray = bestSumValueArray
            continue
        } // 2nd definition
   
        if bestSumValueArray.count < returningArray.count{
            returningArray = bestSumValueArray
        }
        print(returningArray) // 2nd variable
    }
    print(returningArray) // 1st variable
    
}
  • Related