use of string crashing application.
temp is Normal string and strStartDate is also string from Date.
.h file
NSString *temp;
NSString *strStartDate
int status;
.m file
-(void)viewDidLoad
{
[super viewDidLoad];
status = 1;
strStartDate=[[NSString alloc]init];
[self stateChanged];
}
-(void)stateChanged
{
switch(status)
{
case 0:
NSLog(@"%@",temp);
NSLog(@"Start Date : %@",strStartDate);
break;
case 1:
temp=[[NSString alloc]initWithString:@"Temp is here"];
chargeStartDate=[[NSDate date] retain];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/YYYY"];
strStartDate = [dateFormat stringFromDate:chargeStartDate];
NSLog(@"string of start date : %@",strStartDate);
[timeFormat release];
[dateFormat release];
break;
}
}
temp is Normal string and strStartDate is also string from Date.
strings temp and strStartDate both allocating same place, both are class variable, and print same place but in case 0 temp is print while strStarDate crashing. Why?
I know this is simple but, i can’t understand. Please help me.
You need to do a retain on strStartDate after initializing it with stringFromDate. The result of that call will be auto released so you need a retain to stop the object being freed.