ios - Comparing times between multiple dates in NSMutableArray NSDateComponent only returns the value of the first date -


basically have array can contain multiple nsmanagedobjects i'm trying sort through these objects , ones have start date want compare time between start date , or start date , end date if set. lastly set timer refresh information in second.

the problem having when comparing times time returns value of first object start date. if add value start date time sets 0 , starts on when want add them together.

if need more information let me know

i using for(object *obj in array) before seemed have more problems

int time = 0; if([_ttimes count] != 0){      for(int i=0; < [_ttimes count]; ++i){         ttime *ttime = [_ttimes objectatindex:i];         nslog(@"time%i", i);         if(ttime.sdate){             nscalendar *cal = [nscalendar currentcalendar];             nsdate *date = [nsdate date];             if(ttime.edate){                 date = ttime.edate;             }             nsdatecomponents *component = [cal components:nssecondcalendarunit fromdate:ttime.sdate todate:date options:0];             int tmptime = [component second];             time = time + tmptime;         }     }     _tickettimer = [nstimer scheduledtimerwithtimeinterval:1 target:self selector:@selector(totalworktime) userinfo:nil repeats:no]; } 

changed method this:

-(void)totalworktime{ double time = 0; if([_ttimes count] != 0){ for(ttime *ttime in _ttimes){ nsdate *date = [nsdate date]; if(ttime.edate){ date = ttime.edate; } nstimeinterval timerint = -[ttime.sdate timeintervalsincedate:date]; time = time + timerint; } nslog(@"time:%f", time); } }

this seems return more accurate time thank zaph still down not solve problem time += timerint not work right number resets every time add new object , returns value last object added.

the problem nsdatecomponents components:nssecondcalendarunit returns 0-59. (there rare exceptions)

further, since firing timer every second there chance second (0-59) same. check add logging after if(ttime.sdate):

nslog(@"ttime.sdate: %@", ttime.sdate); 

and see if date same of seconds same.

instead of this:

nsdatecomponents *component = [cal components:nssecondcalendarunit fromdate:ttime.sdate todate:date options:0]; int tmptime = [component second]; time = time + tmptime; 

try this:

nstimeinterval tmptime = -[ttime.sdate timeintervalsincedate:date]; time += tmptime; 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -