iphone - Accurate timing with NSDateComponents -


i trying build countdown, displays amount of seconds go.

the example below set 10 seconds, output 8 seconds.

nsdate *currentdate = [nsdate date]; nsdate *finishdate = [nsdate datewithtimeinterval:10 sincedate:currentdate];  nscalendar *calendar = [[nscalendar alloc] initwithcalendaridentifier:nsgregoriancalendar];  nsdatecomponents *components = [calendar components:nssecondcalendarunit|nsminutecalendarunit|nshourcalendarunit | nsdaycalendarunit| nsmonthcalendarunit | nsyearcalendarunit                                            fromdate:currentdate                                              todate:finishdate                                             options:0]; nsinteger hour_ = [components hour];    nsinteger minute_ = [components minute]; nsinteger second_ = [components second];  

log

currentdate = 2012-06-05 03:13:10 +0000 finishdate_ = 2012-06-05 03:13:19 +0000 second_ = 8 

well, copied , pasted code. added these lines bottom:

nslog(@"%@", currentdate); nslog(@"%@", finishdate); nslog(@"%d, %d, %d", hour_, minute_, second_);  

it worked perfectly, using xcode 4.3.2 on ipad simulator running ios 5.1:

2012-06-05 00:19:34.341 testing app[59813:fb03] 2012-06-05 04:19:34 +0000 2012-06-05 00:19:34.342 testing app[59813:fb03] 2012-06-05 04:19:44 +0000 2012-06-05 00:19:34.343 testing app[59813:fb03] 0, 0, 10 

are using different versions?


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -