Ok, so I keep getting a EXC_BAD_ACCESS error which I am guessing is alot like a seg fault error in C.
My book isn’t real specific on some of these details, so I need some help.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDateComponents *comps = [[NSDateComponents alloc ]init];
[comps setYear:1984];
[comps setMonth:7];
[comps setDay:18];
[comps setHour:0];
[comps setMinute:0];
[comps setSecond:0];
NSCalendar *g =[[NSCalendar alloc ]init];
NSDate *dateofbirth = [g dateFromComponents:comps];
NSDate *now = [NSDate date];
double timeSince = [now timeIntervalSinceDate:dateofbirth ];
NSLog(@"your age %@", timeSince);
[pool drain];
return 0;
}
I think I am running into trouble because I am initializing and allocating memory within the pool. Is this correct?
The “%@” in your NSLog string expects an object, but you have passed in a double. Try:
Specifically, “%@” tries to call -message on the argument. Since your argument is a double, the runtime is ultimately trying to treat the double as a pointer, and dereference it (resulting in your EXC_BAD_ACCESS).
As far as memory management is concerned, you are leaking
compsandg. At the end of the program, add a:Although this isn’t causing your error (nor is it really harming anything, as you are exiting and the OS is reclaiming the memory. As you learn Objective-C, however, not releasing your objects in other places will result in leaks)