比较iOS游戏中每日奖励的两个日期

时间:2013-06-25 12:06:00

标签: iphone ios date nsdate

我正在实施“每日奖励”方法,以便在我的游戏中每天给出一些硬币。

我有一个包含实际日期和时间的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。

我认为问题可能在于两个日期的比较。

有人可以告诉我是否有问题吗?

4 个答案:

答案 0 :(得分:3)

使用以下内容。

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

date1和date2是NSdate个对象。

答案 1 :(得分:3)

NSDate有比较日期的方法。 他们在这里:

  

isEqualToDate:返回一个布尔值,指示是否给定   object是一个NSDate对象,与接收者完全相同。

     

- (BOOL)isEqualToDate:(NSDate *)anotherDate

     

laterDate:返回接收者的后一个和另一个给定的日期。

     

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

     

earlyDate:返回接收者的前一个和给定的另一个   日期。

     

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

答案 2 :(得分: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");

答案 3 :(得分: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;

这是实现的代码。