ios - UIDatePicker and date not storing properly -


i have ibaction method tied uitoolbarbutton runs following code.

the uidatepicker setup show time user. therefore, picker randomly chooses date: date today, date tomorrow. want picker choose time selected (but in future). e.g. if user picks 6:00am @ 10:00pm on jan 1st, want date store 6:00 on jan 2nd.

the following if should that, 50% of time nslog returns 1 answer , 50% of time returns other.

if (self.picker.date >= [nsdate datewithtimeintervalsincenow:0]) {     nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults];     nsdate *pickerdate = self.picker.date;     [defaults setobject:pickerdate forkey:@"pickerdate"];     [defaults synchronize];     nslog(@"the date more now. pickerdate %@",pickerdate); } else {     nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults];     nscalendar*       calendar = [[nscalendar alloc] initwithcalendaridentifier: nsgregoriancalendar];     nsdatecomponents* components = [[nsdatecomponents alloc] init];     components.day = 1;     nsdate * pickerdate = [calendar datebyaddingcomponents: components todate: self.picker.date options: 0];     [defaults setobject:pickerdate forkey:@"pickerdate"];     [defaults synchronize];     nslog(@"the date less now. pickerdate %@",pickerdate); } 

edit: code modified, logic backwards problem remains.

you can't use >= (or similar) operator compare 2 nsdate objects. these objects. must use compare: method compare 2 dates. have comparing see if 1 object pointer greater or equal object pointer. not want.

the proper replacement be:

if ([self.picker.date compare:[nsdate date]] != nsorderascending) { 

note [nsdate date] gives same result [nsdate datewithtimeintervalsincenow:0].


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -