Skip to content

Instantly share code, notes, and snippets.

@norio-nomura
Created March 18, 2015 15:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norio-nomura/9e81febbb9484b3ce524 to your computer and use it in GitHub Desktop.
Save norio-nomura/9e81febbb9484b3ce524 to your computer and use it in GitHub Desktop.
SequenceOf<T>とGeneratorOf<T>を使う
import Foundation
func dates(#matchingComponents: NSDateComponents, var startDate: NSDate = NSDate(), endDate: NSDate = NSDate.distantFuture() as! NSDate) -> SequenceOf<NSDate> {
return SequenceOf<NSDate> { _ -> GeneratorOf<NSDate> in
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)
return GeneratorOf<NSDate> {
if let nextDate = calendar.nextDateAfterDate(startDate, matchingComponents: matchingComponents, options: .MatchStrictly)
where nextDate.compare(endDate) != .OrderedDescending {
startDate = nextDate
return startDate
}
return nil
}
}
}
let calendar = NSCalendar.currentCalendar()
let components = NSDateComponents()
components.month = 2
components.day = 29
let hundredYearsLater = calendar.dateByAddingUnit(.CalendarUnitYear, value: 100, toDate: NSDate(), options: nil)
for feb29 in dates(matchingComponents: components, startDate: NSDate(), endDate: hundredYearsLater!) {
println(feb29)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment