1

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.

Bista
  • 7,869
  • 3
  • 27
  • 55
SashaZ
  • 393
  • 7
  • 20
  • http://stackoverflow.com/questions/25714985/how-to-determine-if-one-array-contains-all-elements-of-another-array-in-swift – Bista Oct 05 '16 at 10:05

1 Answers1

2

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

if hbiValidArray.contains(arrayOfDates[item])

to

if hbiValidArray.contains(item)
Jim Matthews
  • 1,181
  • 8
  • 16