Reputation: 4552
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"];
Upvotes: 0
Views: 587
Reputation: 96159
(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
NSDate *lastTurn = match.lastTurn;
NSLog(@"className of lastTurn is %@", [lastTurn className]);
prints className of lastTurn is __NSCFString
and 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:
The sole primitive method of NSDate, timeIntervalSinceReferenceDate, provides the basis for all the other methods in the NSDate interface.Which means that
timeIntervalSinceReferenceDate
is called on both [NSDate date]
and lastTurn
.-[__NSCFString timeIntervalSinceReferenceDate]
states (if I'm not mistaken) that the selector timeIntervalSinceReferenceDate
has been sent to an instance of __NSCFString
, i.e. a "flavour" of NSString, not an instance of NSDate.[NSDate date]
is most likely an instance of NSDate there's only lastTurn
left.
My guess is that all entries in responseDict
are strings and a simple NSDate *lastTurn = match.lastTurn;
doesn't do any conversion, not even (duck-)type-checking.
Upvotes: 3