Skip to content

Instantly share code, notes, and snippets.

@hekt
Created November 2, 2014 13:43
Show Gist options
  • Save hekt/23b4148b329268e1343b to your computer and use it in GitHub Desktop.
Save hekt/23b4148b329268e1343b to your computer and use it in GitHub Desktop.
lottery プロジェクト の Swift 版 (『MAC OS X COCOA プログラミング』3,4章)
import Foundation
class LotteryEntry: NSObject {
var entryDate: NSDate
var firstNumber: Int
var secondNumber: Int
init(initWithEntryDate theDate: NSDate) {
entryDate = theDate
firstNumber = random() % 100 + 1
secondNumber = random() % 100 + 1
}
func setEntryDate(date: NSDate) -> Void {
entryDate = date
}
func description() -> NSString {
var df = NSDateFormatter()
df.timeStyle = .NoStyle
df.dateStyle = .MediumStyle
// df.dateFormat = "YYYY-MM-dd"
return "\(df.stringFromDate(entryDate)) = \(firstNumber) and \(secondNumber)"
}
}
import Foundation
let now = NSDate()
let cal = NSCalendar.currentCalendar()
var weekComponents = NSDateComponents()
srandom(CUnsignedInt(time(nil)));
var array: [LotteryEntry] = []
for var i = 0; i < 10; i++ {
weekComponents.setValue(i, forComponent: NSCalendarUnit.CalendarUnitWeekOfYear)
if let iWeeksFromNow = cal.dateByAddingComponents(weekComponents, toDate: now, options: NSCalendarOptions(0)) {
let newEntry = LotteryEntry(initWithEntryDate: iWeeksFromNow)
array.append(newEntry)
}
}
for entryToPrint in array {
NSLog("%@", entryToPrint)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment