//h file
struct runSTRUCT{
NSDate *RunDateTime;
} ;
//m file
struct runSTRUCT run;
- (void)viewDidLoad {
[super viewDidLoad];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeStyle = NSDateFormatterNoStyle;
[dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm"];
run.RunDateTime = [dateFormatter dateFromString:@"12/02/2012 12:22"];
NSString *dateTimeStr = [dateFormatter stringFromDate:run.RunDateTime];
[dateFormatter release];
}
This all works fine. Then when I click a button and make a string from the date, it gives me EXC_BAD_ACCESS.
-(IBAction)respondButtonPressed:(id)sender{
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
dateFormatter2.timeStyle = NSDateFormatterNoStyle;
[dateFormatter2 setDateFormat:@"MM/dd/yyyy HH:mm"];
NSString *dateTimeStr = [dateFormatter2 stringFromDate:run.RunDateTime];
[dateFormatter2 release];
}
When I look in the console and print description, it generally shows 1 of two things:
-
The program being debugged was signaled while in a function called from GDB.
GDB has restored the context to what it was before the call.
To change this behavior use “set unwindonsignal off”
Evaluation of the expression containing the function (CFShow) will be abandoned.
The program being debugged was signaled while in a function called from GDB.
GDB has restored the context to what it was before the call.
To change this behavior use “set unwindonsignal off”
Evaluation of the expression containing the function (CFShow) will be abandoned. -
Or “run.runDateTime” is some random variable
EDIT: I’m not sure if I put these in the right place because it still doesn’t work(I probably didn’t).
- (void)viewDidLoad {
[super viewDidLoad];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeStyle = NSDateFormatterNoStyle;
[dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm"];
run.RunDateTime = [dateFormatter dateFromString:@"12/02/2012 12:22"];
NSString *dateTimeStr = [dateFormatter stringFromDate:run.RunDateTime];
[run.RunDateTime retain];
[dateFormatter release];
}
-(IBAction)respondButtonPressed:(id)sender{
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
dateFormatter2.timeStyle = NSDateFormatterNoStyle;
[dateFormatter2 setDateFormat:@"MM/dd/yyyy HH:mm"];
NSString *dateTimeStr = [dateFormatter2 stringFromDate:run.RunDateTime];
[dateFormatter2 release];
}
I also have a couple of NSStrings in the struct and they work fine.
You need to retain RunDateTime after you assign/create it from the date formatter.
Make sure to release it in your dealloc!