Can't get NSDate to work correctly
having a strange issue, must be something I'm just not seeing.
I set up a variable in the .h
NSDate *checkIn;
@property (nonatomic, retain) NSDate *checkIn;
I'm setting a variable to todays date in the initWithNibName:
checkIn = [NSDate date];
I also did synthesized it as well. Now later on in my program I use it to build a tablecell with the following line
cell.textLabel.text = [dateFormatter stringFromDate:checkIn];
This line kills the simulator, BAD_EXEC. If I put in a checkIn = [NSDate date]; above it, it works fine. So I'm thinking the variable is开发者_如何学编程n't being stored from when I set it in the initWithNibName:
Not sure why though, as my strings I do the same way are all working fine from method to method. What am I missing?
Using
checkIn = [NSDate date];
is a direct assignment and therefore, is released during the autorelease phase.
You want to use your property by making use of dot notation.
self.checkIn = [NSDate date];
This is make use of the property attributes (specifically retain) and prevent the date to be released to 0.
精彩评论