I am trying to append a value into an optional array.
My code for the empty array is the following
var correctCards: [Int]? = nil
In viewWillAppear I have
let matchedIndex = (numbers.index(of: attackNumber) as! Int)
if correctCards == nil {
correctCards = []
}
if matchedIndex == 4 {
correctCards!.append(matchedIndex)
} else if matchedIndex ==5 {
correctCards!.append(matchedIndex)
} else if and so on...
When I run my code I can confirm that [4] is inside the array. However when I move to a different viewcontroller and comeback to this viewcontroller the value is is replaced by the new matchedIndex for example [5]. I would like the array to build itself up like [4,5]. What is the problem with my code?