dateByAddingYears problem


#1

So I’ve gone through and checked all the code probably ten times now and I still can’t get this to work. hopefully someone can help me out. Here’s the debugger console.

Which makes me believe the error is here:

[code]// Seed the random number generator
srandom(time(NULL));
NSMutableArray *array;
array = [[NSMutableArray alloc] init];

int i;
for (i = 0; i < 10; i++){
	
	// Create a date/time object that is 'i' weeks from now
	NSCalendarDate *iWeeksFromNow;
	iWeeksFromNow = [now dateByAddingYears:0
									months:0
									  days:(i * 7)
									 hours:0 
								   minutes:0 
								   seconds:0];[/code]

But I can’t seem to figure out what I need to do. Thanks for the help,
Jay


#2

Your error message says that you are sending dateByAddingYears:months:days:hours:minutes:seconds: to an autorelease pool. ‘now’ is not properly initialized. Check the line:

now = [[NSCalendarDate alloc] init];

Side-note: Yes, I know NSCalendarDate has been deprecated, but it makes such a great example. I guess I’ll need to rip it out for the fourth edition.


#3

Aaron, thank you very much for your help. It’s always great to have the author help you out.

The code for lottery.m, according to page 49 should read

//Create the date object NSCalendarDate *now = [[NSCalendarDate alloc] init];
your post suggested that was the problem area so I edited the code to this:

NSCalendarDate *now; now = [[NSCalendarDate alloc] init];
and it worked! So thank you!

I agree that NSCalendarDate does make a good example. But as you mentioned because of it’s deprecation you will probably have to change the chapter for the 4th edition. I’m sure you will be able to find a worthy replacement


#4

[quote=“moltzvt”]Aaron, thank you very much for your help. It’s always great to have the author help you out.

The code for lottery.m, according to page 49 should read

//Create the date object NSCalendarDate *now = [[NSCalendarDate alloc] init];
your post suggested that was the problem area so I edited the code to this:

NSCalendarDate *now; now = [[NSCalendarDate alloc] init];
and it worked! So thank you!

I agree that NSCalendarDate does make a good example. But as you mentioned because of it’s deprecation you will probably have to change the chapter for the 4th edition. I’m sure you will be able to find a worthy replacement[/quote]

Sorry moltzvt, but that doesn’t make sense as both code blocks above can be considered as equal and I’m pretty sure they’ll produce the same assembler output and thus, the same binary code, so this is not what’s causing the error you showed earlier

Could you please post the whole .m file that’s throwing some uncaught exceptions?

EDIT: some rewording


#5

This morning when I tried the code as

NSCalendarDate *now = [[NSCalendarDate alloc] init];
it didn’t work. but when I wrote it as

NSCalendarDate *now; now = [[NSCalendarDate alloc] init];
it worked fine. I have since complete chapter 3 and I just retried the original code and it work perfectly. So I’m not sure what the problem was, but it’s working now! Sorry for any trouble and thanks for the help!


#6

I’m new to the forum…I’ve been going through the code examples in the book, “Programming for Mac OS X (3rd Edition)”.
Since NSCalendarDate is deprecated or no longer available for use, which function or method would you recommend using
in it’s place?

I’m new to object programming so please present, in code, a replacement method that would replace NSCalendarDate otherwise
I can’t get the “lottery” program to work and continue moving forward through the book.

Thanks,

Gary