I'm trying to do something that should be very simple. I have an array of Dates and from this I simply want to extract a Date from a given index.
In Python I could do something like this:
print(type(myArray[i]
and it would come back with some kind of Date type.
However when I grab a value from my array in Swift it won't accept it as a Date input for my next function because its type is ArraySlice.
My question is simply this - how can I 'unwrap' my ArraySlice and actually get the Date object inside?
var cycleStartsArr: Array<Date> = Array()
// Some code that puts dates into the array
var gaps: Array<Int> = Array() // Will contain list of the gaps in days
for i in [1..<cycleStartsArr.count] {
let startDate = cycleStartsArr[i]
let endDate = cycleStartsArr[i-1]
let gap = Calendar.current.dateComponents([.day], from: startDate.day!, to:endDate.day! // Throws Cannot convert value of type 'ArraySlice<Date>' to expected argument type 'Date'.
gaps.append(gap)
}
CodePudding user response:
[1..<cycleStartsArr.count]
is an array OF rangeS.
You meant
cycleStartsArr.indices.dropFirst()
Or better yet,
import Algorithms
for (endDate, startDate) in cycleStartsArr.adjacentPairs() {