신속한 n00b 질문이 있습니다. 배열에서 요소를 제거 할 수없는 이유를 이해하는 데 어려움을 겪고 있습니다.스위프트 배열에서 removeAtIndex()가 작동하지 않습니다.
내가 처음 내가 필요로하는 값만을 포함하도록 두 번 필터링: 배열의 크기에 따라 달라집니다 while 루프 내부 선 아래로,
let filteredShowtimes = movieShowtimes.filter{$0.dateTime.laterDate(newStartTime!).isEqualToDate($0.dateTime)}
var furtherFilteredShowtimes = filteredShowtimes.filter{$0.endTime.earlierDate(endTime!).isEqualToDate($0.endTime)}
을 그리고 -하지만 반복하지 않습니다 또는 수정하기 - 첫 번째 요소를 다음과 같이 제거하려고합니다.
furtherFilteredShowtimes.removeAtIndex(0)
요소 수는 동일하게 유지됩니다.
내가 무엇을 놓치고 있는지 알 수 있습니까? else
에,
while(furtherFilteredShowtimes.count > 0) {
println("showtime \(furtherFilteredShowtimes.first!.dateTime)")
//if the start time of the movie is after the start of the time period, and its end before
//the requested end time
if (newStartTime!.compare(furtherFilteredShowtimes.first!.dateTime) == NSComparisonResult.OrderedAscending) && (endTime!.compare(furtherFilteredShowtimes.first!.endTime) == NSComparisonResult.OrderedDescending) {
let interval = 1200 as NSTimeInterval
//if the matching screenings dict already contains one movie,
//make sure the next one starts within 20 min of the previous
//one
if(theaterMovies.count > 1 && endTime!.timeIntervalSinceDate(newStartTime!) < interval {
//add movie to the matching screenings dictionary
println("we have a match with \(movies[currentMovie.row].title)")
theaterMovies[furtherFilteredShowtimes.first!.dateTime] = movies[currentMovie.row].title
//set the new start time for after the added movie ends
newStartTime = movieShowtimes.first!.endTime
//stop looking for screenings for this movie
break
}
else if(theaterMovies.count == 0) {
//add movie to the matching screenings dictionary
theaterMovies[furtherFilteredShowtimes.first!.dateTime] = movies[currentMovie.row].title
println("we have a new itinerary with \(movies[currentMovie.row].title)")
//set the new start time for after the added movie ends
newStartTime = furtherFilteredShowtimes.first!.endTime
//stop looking for screenings for this movie
break
}
}
else { //if the showtime doesn't fit, remove it from the list
println("removing showtime \(furtherFilteredShowtimes.first!.dateTime)")
furtherFilteredShowtimes.removeAtIndex(0)
}
}
while 루프의 코드를 표시 할 수 있습니까? –
그래, 반복하지 않으면 루프에 뭔가 문제가있을거야 ... –