mardi 4 août 2015

swift: "for" loop and "if" statement return four arrays with one element instead of one array with four elements

I have an array of NSDate. I am filtering this array with a timeIntervalSinceDate to only keep specific ones. After looping through the array, and submitting an "if" statement to compare the elements in the array with others, I intend to append the desired ones into a new array.

However, after the whole process, it returns an array of one element every time, returning all the right elements, but in separate arrays every time. Instead of having one array of four elements, I end up with four arrays of one.

Here are the codes:

      var range = -15...15

      if (self.element1 == self.element11) && (self.element2 == self.element22) && contains(range, components.second) {

      self.dateX.append(self.date2)

      var countArray = [NSDate]()

     if NSDate().timeIntervalSinceDate(self.date2) <= 86400 {

                                                for n in self.dateX {

                                                    if n == self.date2 {

                                                     countArray.append(n)

                                                    } } }



}

Do you have any idea of what might cause this issue ? Why would it return different arrays for each element every time instead of one array with all ?

Thank you very much,

Aucun commentaire:

Enregistrer un commentaire