2014-12-17 2 views
0

신속한 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) 
    } 

}

+1

while 루프의 코드를 표시 할 수 있습니까? –

+0

그래, 반복하지 않으면 루프에 뭔가 문제가있을거야 ... –

답변

0

에만 removeAtIndex(0) 장소 하나의 말 :

여기에 전체 코드입니다.

따라서 발생하지 않으면 else이 실행되지 않아 회선이 실행되지 않고 있음을 의미합니다. 대신 if이 실행됩니다.

그리고 각 if의 끝에 break이므로 while 루프의 끝입니다!

즉, 첫 번째 두 개의 중첩 된 if 조건이 성공했다고 가정합니다. 귀하의 구조는 다음과 같다 : 두 if 조건이 성공하면, 그 말 그래서

while(furtherFilteredShowtimes.count > 0) { 
    if (something) { 
     if (somethingelse) { 
      break 

break 수단은 while에서 점프! 우리는 반복하지 않습니다. 우리는 결코 removeAtIndex()에 결코 가지 않을 것이다.

+0

예. 그것이 내가 구조화 한 방법입니다. 조건이 의미가없고 while 루프가 계속 될 때만 제거 할 항목이었습니다. 콘솔 출력물에서 else 절에 도달하면 제거가 수행되지 않습니다. – wonderv

+0

콘솔에 해당 정보를 인쇄하는 코드를 코드에 추가 할 수 있습니까? 나는 당신이 이것을 어떻게 결정하는지에 관해서 우스운 무엇인가 의심한다. – matt

관련 문제