Search code examples
arraysswiftdateios-charts

Swift Check if array is contained in another array


I am trying to check if a date is contained in an array so I can give a score for iOS-Charts. I have an array of past x days and an array of valid dates. If the date is not valid then score = 0 else the score is calculated. However I don't think I have got the for in section correct.

func hbiScoreOnCertainDayForLast(days: Int, index: Int) ->(Double){
  let hbiValidArray = datesWithSavedDataForLast(days: days)

  var answer = [Double]()
  let arrayOfDates = today.arrayOfDateForPreviousDays(numberOfDays: days)

  for item in arrayOfDates {
    if hbiValidArray.contains(arrayOfDates[item]){
      answer.append((harvyeBradshawModel.harveyBradshawIndexScore(item as Date)!.doubleValue))
    } else {
      answer.append(0.0)
    }
  }

  var x = Double()
  if answer.count > 0 {
    x = answer[index]
  }
  return x
}

Array of Dates prints:

[2016-10-04 09:19:23 +0000, 2016-10-03 09:19:23 +0000, 2016-10-02 09:19:23 +0000, 2016-10-01 09:19:23 +0000, 2016-09-30 09:19:23 +0000, 2016-09-29 09:19:23 +0000, 2016-09-28 09:19:23 +0000]

I expect to make an array of scores for these days with 0 for not valid scores and return the double for index.


Solution

  • item is a member of arrayOfDates, not an index into it, so try changing

    if hbiValidArray.contains(arrayOfDates[item])
    

    to

    if hbiValidArray.contains(item)