0

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?

mugx
  • 9,869
  • 3
  • 43
  • 55
Satsuki
  • 2,166
  • 5
  • 17
  • 33
  • Keep data model outside your contollers and dont reset it when changing contillers. – Sulthan Nov 20 '17 at 07:12
  • Hello I don't get your comment . dont reset it when changing controllers. I'm not resetting them.. – Satsuki Nov 20 '17 at 07:14
  • 1
    how do you 'come back to this viewcontroller' ? Sounds like you are creating a new instance of the initial controller – Russell Nov 20 '17 at 07:14
  • 1
    @Satsuki You can use a Singleton class to use a single property in both view controllers – Leo Dabus Nov 20 '17 at 07:16
  • 1
    Btw no need to force cast your array index as Int. The array index it is already an `Int` – Leo Dabus Nov 20 '17 at 07:19
  • 1
    Also you can make your correctCards non optional and initialize it with an empty array. – Leo Dabus Nov 20 '17 at 07:25
  • @Russell Hello I am coming back using a present modally segue. – Satsuki Nov 20 '17 at 07:48
  • @Satsuki Another option is to add an observer to your original view controller and post a notification at the other one. You can send an object (matchedIndex) with your notification. https://stackoverflow.com/questions/30328452/how-to-pass-multiple-values-with-a-notification-in-swift/30329316?s=5|0.0000#30329316 – Leo Dabus Nov 20 '17 at 07:56

2 Answers2

1

You have to maintain the array in a shared instance. Then only the scope of the variable will be not be deallocated.

class DataManager: NSObject {

  static let sharedManager = DataManager()
  var correctCards : [Int] = []

}

When appending data, you can append something like below:

 DataManager.sharedManager.correctCards.append(matchedIndex)
rajtharan-g
  • 432
  • 5
  • 14
0

try to move your code inside viewDidLoad (instead of viewWillAppear):

override func viewDidLoad() {
  super.viewDidLoad()

  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...
}
mugx
  • 9,869
  • 3
  • 43
  • 55
  • that won't work - the whole point is the array is meant to build up every time you return to the initial controller - this will only append a single entry – Russell Nov 20 '17 at 07:18
  • 1
    @Russell This might work, because the viewcontroller is not deinit when it present new vc, viewDidLoad is not called if he go back from presented vc, viewWillAppear does, still, if he dismiss this one, then the value will be lost, safest way is still making it singleton or move to different class – Tj3n Nov 20 '17 at 07:33