ios - loading upcoming events by comparing their dates with the calendar date -


i have found below code online comparing 2 dates (the date of calendar , date) modifying code compares calendar date date of event date here key each event within nsarray (json file) .. how use later nspredicate can load events components (difference between 2 dates in days) @ least more 1 day since im going have uibutton called future events when user click on it, future events should loaded .. appreciate , code examples since im new objective c , xcode. thanks.

@implementation homeviewcontroller {     nsarray *_events; }  - (bool)issameday:(nsdate*)date {     nscalendar* calendar = [nscalendar currentcalendar];     nsdate *currdate = [nsdate date];     //date = [_events objectatindex:date];      unsigned unitflags = nsyearcalendarunit | nsmonthcalendarunit |  nsdaycalendarunit;     nsdatecomponents* comp1 = [calendar components:unitflags fromdate:date];     nsdatecomponents* comp2 = [calendar components:unitflags fromdate:currdate];     nsdatecomponents *components = [calendar components:unitflags                                                fromdate:currdate                                                  todate:date                                                 options:0];      nslog(@"days between dates: %@", components);      return [comp1 day]   == [comp2 day] &&     [comp1 month] == [comp2 month] &&     [comp1 year]  == [comp2 year]; } 

to number of days remaining date now, can use method below:

-(long)daysremaining:(nsdate*) date {     nsdate *now = [nsdate date];     nstimeinterval datediff = [date timeintervalsincenow] - [now timeintervalsincenow];     cgfloat numberofdaysremaining = (datediff/(60*60*24));     return ceil(numberofdaysremaining); } 

so can call number of days remaining this:

nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdateformat:@"yyyy-mm-dd"]; nsdate *datetocompare = [dateformatter datefromstring: @"2014-02-08"];  long numberdays = [self daysremaining:datetocompare]; 

for case, result 2 days remaining. note negative result indicates passed n days.

to filter array (2 days remaining events) making copy of it:

nsarray* dates = [[nsarray alloc] initwithobjects:     [dateformatter datefromstring: @"2014-02-08"],     [dateformatter datefromstring: @"2014-02-05"],     [dateformatter datefromstring: @"2014-02-10"],     [dateformatter datefromstring: @"2014-02-08"], nil];  nsarray *filteredarray = [dates filteredarrayusingpredicate:     [nspredicate predicatewithblock:         ^bool(id evaluatedobject, nsdictionary *bindings) {             long daysremaining = [self daysremaining:(nsdate*)evaluatedobject];             return daysremaining == 2; // if somemethod returns yes, object kept         }     ]]; 

to filter nsmutablearray in place:

[mutablearray filterusingpredicate:[nspredicate predicatewithblock:     ^bool(id evaluatedobject, nsdictionary *bindings) {         long daysremaining = [self daysremaining:(nsdate*)evaluatedobject];         return daysremaining == 2; // if somemethod returns yes, object kept     } ]]; 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -