A Simple About Manual Reference Counting



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

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


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


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


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.


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.


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.