Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing two dates for daily reward in iOS game
    text
    copied!<p>I was implementing a 'daily reward' method in order to give some coins every day in my game. </p> <p>I have a json that contains the actual date and time. </p> <p>The issue is that I don't know how to compare the dates.</p> <p>Here is my code -</p> <pre><code>@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 </code></pre> <p>So, when I open my app and call this method, my app crashes for some reasone.</p> <p>I think the problem might be on the comparison of the two dates. </p> <p>Can someone tell me if there is something wrong?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload