Hello, i'm working out of Aaron Hillegass' book (very popular around here). I'm only on Chapter 3, but I already have questions regarding retain/releasing properly.
In my lottery.m main() method I have the following loop:
for (i=0; i < 10; i++)
{
// Create a new instance of LotteryEntry
newEntry = [LotteryEntry new];
[newEntry prepareRandomNumbers];
// Create a date/time object that is i weeks from now
[newEntry setEntryDate: [now dateByAddingYears:0 months:0 daysi*7) hours:0 minutes:0 seconds:0]];
// Add the LotteryEntry object to the array
[array addObject:newEntry];
// Decrement the retain count of the lottery entry
[newEntry release];
}
My LotteryEntry.m's setEntryDate() method looks like this:
- (void)setEntryDateNSCalendarDate *)date
{
[date retain];
[entryDate release];
[date setCalendarFormat"%b %d, %Y"];
entryDate = date;
}
This does NOT make logical sense at all. Here's what I don't understand.
1. WHY do I do [date retain]? What's the point of increasing the retain value here?
2. Since the for-loop will call setEntryDate() 10 times, i'm releasing entryDate 10 times. How is that POSSIBLE, considering that I haven't called retain on entryDate 10 times.
Thanks,
cz
In my lottery.m main() method I have the following loop:
for (i=0; i < 10; i++)
{
// Create a new instance of LotteryEntry
newEntry = [LotteryEntry new];
[newEntry prepareRandomNumbers];
// Create a date/time object that is i weeks from now
[newEntry setEntryDate: [now dateByAddingYears:0 months:0 daysi*7) hours:0 minutes:0 seconds:0]];
// Add the LotteryEntry object to the array
[array addObject:newEntry];
// Decrement the retain count of the lottery entry
[newEntry release];
}
My LotteryEntry.m's setEntryDate() method looks like this:
- (void)setEntryDateNSCalendarDate *)date
{
[date retain];
[entryDate release];
[date setCalendarFormat"%b %d, %Y"];
entryDate = date;
}
This does NOT make logical sense at all. Here's what I don't understand.
1. WHY do I do [date retain]? What's the point of increasing the retain value here?
2. Since the for-loop will call setEntryDate() 10 times, i'm releasing entryDate 10 times. How is that POSSIBLE, considering that I haven't called retain on entryDate 10 times.
Thanks,
cz