BOOL
您可以存储NSDate
上次随机播放的日期/时间 ( ),而不是存储 a 。
然后通过比较存储的日期和viewDidAppear
.
请参阅NSTime
文档:http: //developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html
和NSDateFormatter
文档:
https ://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003643
更新:
根据要求,这里是一些示例代码。诚然,可能有更好的解决方案,但我相信这段代码片段将帮助您解决问题。这样做是检查是否有使用 保存的日期NSUserDefaults
,然后与当前日期进行比较。如果日期不匹配,则打乱数组,然后保存当前日期(再次使用NSUserDefaults
)。(我冒昧地假设时间确实会继续向前发展,所以它不会检查以确保它lastSavedDate
在. )之前currentDate
。)
NSDate *currentDate = [[NSDate alloc] init];
NSDate *lastShuffleDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastShuffleDate"];
// check to see if there is a prior shuffle date
// if there is not, shuffle the array and save the current date
if (!lastShuffleDate) {
NSLog(@"No object set for 'lastShuffleDate'");
//[self shuffleMyArray];
[[NSUserDefaults standardUserDefaults] setObject:currentDate forKey:@"lastShuffleDate"];
return;
}
// set up the date formatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLocale];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSLog(@"Current Date: %@", [dateFormatter stringFromDate:currentDate]);
NSLog(@"Saved Date: %@", [dateFormatter stringFromDate:lastShuffleDate]);
// check to see if the dates are the same by comparing the dates as a string
if (![[dateFormatter stringFromDate:currentDate] isEqualToString:[dateFormatter stringFromDate:lastShuffleDate]]) {
NSLog(@"Dates are different...!");
//[self shuffleMyArray];
} else {
NSLog(@"Dates are the same... (midnight has not passed)");
}
// save the time of the last shuffle
[[NSUserDefaults standardUserDefaults] setObject:currentDate forKey:@"lastShuffleDate"];
在这一点上,您没有真正的理由查看时间,但我已将其包括在内以防您好奇。
// remote dateStyle and set timeStyle to check times
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSLog(@"Current Time: %@", [dateFormatter stringFromDate:currentDate]);
NSLog(@"Saved Time: %@", [dateFormatter stringFromDate:lastShuffleDate]);