I can not get the value of the time interval between two points in time, between endDate [i] and startDate [i + 1] (that is, between the stop and the next start) and that everything is output to the String array.

var startDate = [String]() endDate = [String]() interval = [String]() let date = NSDate() var i = 0 let dateFormatter = DateFormatter() dateFormatter.dateFormat = "HH:mm:ss" // 袧袗袞袠袦袗袝袦 袧袗 小孝袗袪孝 i += 1 let stringDate = dateFormatter.string(from: date as Date) startDate.append(stringDate) // 袙蝎效袠小袥携袝袦 袟袧袗效袝袧袠袝 袠袧孝袝袪袙袗袥袗 KAK ??????????????????????????????????????? let intervalDate = startDate[i + 1] - endDate[i] // ?????????????????? interval.append(intervalDate) print(interval) //袧袗袞袠袦袗袝袦 小孝袨袩 let stringDate = dateFormatter.string(from: date as Date) endDate.append(stringDate) 
  • Either save as timeinterval / date, or convert the string back and then calculate. Or you can write your own solution, which will calculate the difference of two lines in the format "HH: mm: ss" - VAndrJ

1 answer 1

Firstly, var i and its further use look very crutch. Secondly, storing dates and intervals in the String type is a very strange and bad decision. Third, startDate and endDate are String arrays, below you are trying to subtract them. Fourth, use the timeIntervalSinceDate method.

Such use of i and similar handling of indexes will most likely lead you to the index out of bounds exception.