Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert NSDate to NSString?

Tags:

iphone

converting NSDate to NSString creates a memory leak can anyone help.

Here is my 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];
like image 954
leena Avatar asked Jul 01 '11 10:07

leena


People also ask

How do I get today's date in Objective C?

Get Today's Date: NSDate* date = [NSDate date];


6 Answers

As you aren't releasing anything the code creates a memory leak.

NSDate *today = [NSDate date]; //Autorelease
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease]; //Autorelease

[df setDateFormat:@"yyyy-MM-dd"]; // 2017-09-28
dateString = [[df stringFromDate:today] retain];

[df setDateFormat:@"EEEE       MMM dd yyyy"];  // Thursday Sep 28 2017
[dateButton setTitle:[df stringFromDate:today] forState:UIControlStateNormal];

For more details, you can refer to Apple's documentation.

like image 158
Kyle Howells Avatar answered Sep 28 '22 20:09

Kyle Howells


Use

NSDate *today = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"];
dateString = [df stringFromDate:today];
[df release]
like image 27
Nithin Avatar answered Sep 28 '22 20:09

Nithin


Using your 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]];

...you need to release a lot of obj because nothing is in autorelease.

[today release];  -> alloc
[df release]      -> alloc
[dateString release]; -> alloc

Or change to:

NSDate *today = [NSDate date];
NSDateFormatter *df = [NSDateFormatter initWithDateFormat:@"yyyy-MM-dd"];
dateString = [df stringFromDate:today];

with no one release/alloc!

like image 31
elp Avatar answered Sep 28 '22 20:09

elp


The leak is in the DateFormatter that is not being released. This should fix the leak :

[df release];
like image 45
werner Avatar answered Sep 28 '22 19:09

werner


Also, try using ...

[NSDate date]

instead of ...

NSDate* today = [[NSDate alloc] init];

that is a lot of alloc/initing that you are doing there as well... you don't need to alloc/init the NSString either.

like image 30
bdparrish Avatar answered Sep 28 '22 19:09

bdparrish


You Shoud noy alloc or init NSDate object

Try this Code

NSDate *today = [NSDate date];
NSDateFormatter *dt = [[NSDateFormatter alloc]init];
[dt setDateFormat:@"yyyy-mm-dd"];
NSString *str =[NSString stringWithFormat:@"%@",[dt stringFromDate:today]];
NSLog(@"%@",str);
[dt release];

Happy Coding

like image 33
Mehul Mistri Avatar answered Sep 28 '22 20:09

Mehul Mistri