2013-06-25 75 views
0

我正在實施「每日獎勵」方法,以便在遊戲中每天提供一些硬幣。比較iOS遊戲中每日獎勵的兩個日期

我有一個包含實際日期和時間的json。

問題是我不知道如何比較日期。

這裏是我的代碼 -

@implementation ItemRewardManager 

+(NSDate *)dateFromUTCTimeStamp:(NSString *)dateString{ 

    NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
    df.dateFormat = @"yyyy-MM-dd HH:mm:ss"; 
    NSDate *myDate = [df dateFromString: dateString]; 
    NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 
    [df setTimeZone:gmt]; 

    [[NSUserDefaults standardUserDefaults] setObject:myDate forKey:@"lastDatePlayed"]; 
    [[NSUserDefaults standardUserDefaults] synchronize]; 
} 

-(void)getData 
{ 
    NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:dateUrl]]; 
    id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]; 
    NSArray *keys = [jsonObjects allKeys]; 

    // values in foreach loop 
    for (NSString *key in keys) { 
     NSLog(@"%@ is %@",key, [jsonObjects objectForKey:key]); 
    } 
} 

+(void)dailyReward{ 
    NSLog(@"llega al daily"); 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init]; 
    [dateFormat setDateFormat:@"HH:mm:ss zzz"]; 

    NSDate *now = [NSDate date]; 
    NSString *dateString = [dateFormat stringFromDate:now]; 

    if([[[NSUserDefaults standardUserDefaults] objectForKey:@"lastDatePlayed"] isEqualToString: dateString]) 
    { 
     NSLog(@"Actual date is the same as json date"); 
     UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:nil 
                 message:@"You have win 5 coins! Come back tomorrow for more." 
                 delegate:nil 
               cancelButtonTitle:@"Ok" 
               otherButtonTitles: nil] autorelease]; 
     [alert show]; 
     return; 
    } 
} 
@end 

所以,當我打開我的應用程序,並調用這個方法,我的應用程序崩潰的一些reasone。

我認爲這個問題可能是兩個日期的比較。

有人可以告訴我,如果有什麼問題嗎?

+1

我不認爲獎勵是客戶的工作,而是服務器之一。但是你是否已經放棄了你的「lastDatePlayed」?您將其另存爲NSDate對象並將其與NSString進行比較。可能是一個原因。順便說一句,這些結果將永遠不會相等,據我所知:) – geo

+0

@geo你說得對。我沒有比較一些邏輯。我現在不知道每日獎勵的邏輯......你能幫助我嗎? – Vergmort

+1

我會建議讓服務器對獎勵進行計算,只是向您發送通知,您現在可以請求它。我也會讓服務器發送* unix-timestamp *,更容易處理。也用於比較日期使用@ torip3ng的答案。並添加值到一個日期,你可以使用'NSDateComponents'和'NSCalendar'。 ('NSDateComponents * comps = [[NSCalendar currentCalendar]組件:NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:date];')即 – geo

回答

3

使用以下。

if ([date1 compare:date2]==NSOrderedSame) 

日期1和date2是NSdate對象。

+0

我不明白,我應該'dateString'and'date2'代之以'date1'「[[NSUserDefaults的standardUserDefaults]的setObject:指明MyDate forKey:@ 」lastDatePlayed「];」 ? – Vergmort

+0

不是字符串。你有NSDate *現在= [NSDate日期];和lastDate對象。如果將lastDate存儲爲字符串,則將其轉換爲dateObject,如NSDate * now = [dateFormat dateFromString:[[NSUserDefaults standardUserDefaults] objectForKey:@「lastDatePlayed」]]; – Durgaprasad

+0

不,崩潰。終止應用程序由於未捕獲的異常'NSInvalidArgumentException',原因:' - [__ NSDate長度]:無法識別的選擇器發送到實例0x1d587e10' – Vergmort

3

NSDate有比較日期的方法。 這裏,他們是:

isEqualToDate:返回一個布爾值,指示一個給定的 對象是否是一個NSDate對象和正好等於該接收器。

- (BOOL)isEqualToDate:(NSDate *)anotherDate

laterDate:返回接收器和其他特定日期的版本。

- (NSDate *)laterDate:(NSDate *)anotherDate

earlierDate:返回接收器的前面,另一個給 日期。

- (NSDate *)earlierDate:(NSDate *)anotherDate

+0

像這樣? 如果([現在isEqualToDate:serverDate] == TRUE){ NSLog的 (@ 「實際日期相同JSON日期」); UIAlertView中*警報= [[[UIAlertView中頁頭] initWithTitle:無 消息:@ 「!。你必須贏得5個硬幣明天再來吧更多」 代表:無 cancelButtonTitle:@ 「確定」 otherButtonTitles:無]自動釋放]; [alert show]; return; } – Vergmort

+0

需要這些方法來比較和獲取日期對象。如果([date1 compare:date2] == NSOrderedDescending){ NSLog(@「date1晚於date2」); (日期1比較:date2] == NSOrderedAscending){ NSLog(@「date1早於date2」); (@「日期是相同的」);其他{ }其他{ NSLog } – torip3ng

1

嘗試這樣的。它可能會幫助你

NSDate *now = [NSDate date]; // current date 
NSDate *newDate = [dateFormat dateFromString:[[NSUserDefaults standardUserDefaults] objectForKey:@"lastDatePlayed"]]; // server date 

NSComparisonResult result; 

result = [now compare:newDate]; // comparing two dates 

if(result == NSOrderedAscending) 
    NSLog(@"now is less"); 
else if(result == NSOrderedDescending) 
    NSLog(@"newDate is less"); 
else 
    NSLog(@"Both dates are same"); 
+0

我應該把什麼放在''xxxx''上? this:'[[NSUserDefaults standardUserDefaults] setObject:myDate forKey:@「lastDatePlayed」];'? – Vergmort

+0

'[[NSUserDefaults standardUserDefaults] objectForKey:@「lastDatePlayed」]'。把這個和嘗試 –

+0

'NSDate * newDate = [NSDate dateWithString:[[NSUserDefaults standardUserDefaults] objectForKey:@「lastDatePlayed」]];'完整的代碼 –

0

使用此代碼。

NSDate *date = [NSDate date]; 
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; 
[dateFormatter1 setDateFormat:@"MM/dd/yyyy"]; 
NSDate *ServerDate = [dateFormatter1 dateFromString:@"03/01/2013"]; 
[dateFormatter1 release]; 
NSTimeInterval interval = [date timeIntervalSinceDate:ServerDate]; 
int diff=interval/1440; 

這是實施的代碼。