Created
November 2, 2014 13:43
-
-
Save hekt/23b4148b329268e1343b to your computer and use it in GitHub Desktop.
lottery プロジェクト の Swift 版 (『MAC OS X COCOA プログラミング』3,4章)
This file contains 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 | |
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)" | |
} | |
} |
This file contains 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 | |
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