NSDate been released automatically resulting Bad exe error. iPhone
I have spent all day working on this, re-writing this many a times trying to get my head around it.
I have a set of dates declared in the Header
@property (nonatomic, retain)NSDate *time;
On view did load i read a string from a database, convert it to a date and set it to this variable.
NSDateFormatter *dateFormatter =[[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd / MMMM / yyyy"];
time=[dateFormatter dateFromString:datetime];
开发者_高级运维
if i NSLog time i get the date expected (although not the right format). if i then go to use the NSDate further on in the view i get a Bad Exe error, or error due to time being = to nil.
I dont understand it is almost like its not retaining it.
Whilst problem-solving i created a new date within an IBAction and tried to replicate the code above but where it was required and i get the same issue. I assume that it is dateformat issue but i am formatting it the same way it was originally written as a string.
What am i doing wrong? i cant afford to loose another day on this.
Cheer
Dan
You need to the the property as self.time = [dateFormatter dateFromString:datetime];
or [self setTime: [dateFormatter dateFromString:dateTime]]
If you just assign it using = without using self, it won't retain it, it will just transfer the pointer. using dot operator will make it retain it.
The problem is, you're not setting the property. time
isn't using the dot notation there. You either have to write that as (assuming the property exists on self), it one of two ways:
self.time = [dateFormatter dateFromString:datetime];
or
[self setTime: [dateFormatter dateFromString:datetime]];
You need to assign it to your property using
self.time = [dateFormatter dateFromString:datetime];
Otherwise it doesn't get retained
try using self.time , this will go through the generated setter method, which will retain the date, at the moment you are just assigning it to the instance variable.
You need to use: self.time = [dateFormatter dateFromString:datetime];
精彩评论