Created
March 18, 2015 15:04
-
-
Save norio-nomura/9e81febbb9484b3ce524 to your computer and use it in GitHub Desktop.
SequenceOf<T>とGeneratorOf<T>を使う
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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