iphone - How to convert NSDate to NSString? -
converting nsdate
nsstring
creates memory leak can help.
here code:-
nsdate *today = [[nsdate alloc]init]; nsdateformatter *df = [[nsdateformatter alloc] init]; [df setdateformat:@"yyyy-mm-dd"]; datestring = nil; datestring =[[nsstring alloc]initwithstring:[df stringfromdate:today]]; [df setdateformat:@"eeee mmm dd yyyy"]; [datebutton settitle:[df stringfromdate:today] forstate:uicontrolstatenormal]; [df release]; [today release];
it creates memory leak because aren't releasing anything. edit: you've updated code in question i'll updated code.
new code
nsdate *today = [nsdate date]; //autorelease nsdateformatter *df = [[nsdateformatter alloc] init] autorelease]; //autorelease [df setdateformat:@"yyyy-mm-dd"]; datestring = [df stringfromdate:today] retain]; [df setdateformat:@"eeee mmm dd yyyy"]; [datebutton settitle:[df stringfromdate:today] forstate:uicontrolstatenormal];
original answer
nsdate *today = [nsdate date]; //swapped autorelease class method nsdateformatter *df = [[nsdateformatter alloc] init] autorelease]; //will release [df setdateformat:@"yyyy-mm-dd"]; datestring = [[df stringfromdate:today] retain]; //simplified code
for further more detail, can refer documentation here: nsdateformatter
Comments
Post a Comment