を格納する代わりに、最後のシャッフルBOOL
の日付/時刻 ( ) を格納できます。NSDate
次に、 に保存されている日付と現在の日付を比較して、最後のシャッフルから午前 0 時を過ぎたかどうかを確認し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]);