Reputation:
I am making a simple iOS notes application just to learn, and I wanted to add a simple date when the notes are created. Kinda like the default notes.app. This is what I have tried so far:
I have created a label in the storyboard and made it into a property and synthesized it. This is my code
- (void)configureCell:(IdeasTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
...
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat: @"yyyy-MM-dd HH:mm:ss zzz"];
NSDate *dateTmp;
cell.dateLabel.text = [dateFormat stringFromDate:dateTmp];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
It keeps returning an error... any ideas on what is causing the problem? Sorry, for the dumb question, but I have just started iOS dev.
Upvotes: 3
Views: 111
Reputation: 5268
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat: @"yyyy-MM-dd HH:mm:ss zzz"];
NSDate *dateTmp = [NSDate date];
cell.detailTextLabel.text = [dateFormat stringFromDate:dateTmp];
the above code gives me the proper result
Upvotes: 0
Reputation: 726589
The reason you get a crash is that the NSDate* object is not initialized. You need to assign it an instance representing the current date:
NSDate *dateTmp = [NSDate date];
Upvotes: 2