I’m trying to find the time interval since last move in a game. Normally, in objective-c, I do this and it works great:
NSDate *now = [NSDate date];
NSDate *before = [NSDate dateWithTimeInterval:-60 sinceDate:now];
NSTimeInterval interval = [now timeIntervalSinceDate: before];
I store the lastTurn date in mysql database:
timestamp - ON UPDATE CURRENT_TIMESTAMP
When I try to get the date in objective-c, it seems to work:
NSDate *lastTurn = match.lastTurn;
NSLog(@"lastTurn: %@", lastTurn);
Output: lastTurn: 2012-03-21 09:40:32
…however, when I try to do a time interval, I get the -[__NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance error.
NSDate *lastTurn = match.lastTurn;
NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate: lastTurn];
Any suggestions would awesome. Thanks
EDIT
I use ASIHTTPRequest to get the information from the database to the iphone.
The Match class is just a simple class that holds the variables:
@property (nonatomic, strong) NSDate *lastTurn;
etc...
… And I get the variable by doing this:
[request setCompletionBlock:^
{
for (NSDictionary *dict in responseDict)
{
Match *match = [[Match alloc] init];
match.lastTurn = [dict objectForKey:@"lastTurn"];
(disclaimer: I know nothing about objective-c, so this might be complete rubbish, only reading the documentation leads to dangerous superficial knowledge ;-))
My bets are on
prints
className of lastTurn is __NSCFStringand you have to convert that string into an instance of NSDate via e.g. dateWithString before using it like an instance of NSDate.NSDate Class Reference says:
Which means that
timeIntervalSinceReferenceDateis called on both[NSDate date]andlastTurn.The error message
-[__NSCFString timeIntervalSinceReferenceDate]states (if I’m not mistaken) that the selectortimeIntervalSinceReferenceDatehas been sent to an instance of__NSCFString, i.e. a “flavour” of NSString, not an instance of NSDate.Since
[NSDate date]is most likely an instance of NSDate there’s onlylastTurnleft.My guess is that all entries in
responseDictare strings and a simpleNSDate *lastTurn = match.lastTurn;doesn’t do any conversion, not even (duck-)type-checking.