A Simple About Manual Reference Counting


#1

Hi,

Do you think the setter of containedItem that uses MRC correct?

- (void)setContainedItem:(BNRItem *)i { [i retain] containedItem = i [i setContainer:self] [i release] }


#2

Where are you seeing this? Are you using an earlier edition of the book?


#3

I’m sorry about the misunderstanding. My question was if the getter that I wrote is correct or not?


#4

Almost. For a retained property it should look like this:

- (void)setContainedItem:(BNRItem *)i { [i retain] [containedItem release]; containedItem = i [i setContainer:self]; // or [containedItem setContainer:self]; }

The way you wrote it you were releasing the new object instead of the old one.


#5

I’m not knowing the context of what you’re talking about but wouldn’t it be better to avoid side effects in your accessor methods ?
I would always prefer to call setContainer: in the method where I call setContainedItem:.
Please correct me if I’m wrong.


#6

I think context makes a big difference here. In some cases I’m sure you’d be right, but in this one I don’t see a problem with it.