Objective-C Objects Having Each Other as Properties

Posted by mwt on Stack Overflow See other posts from Stack Overflow or by mwt
Published on 2010-03-19T08:14:01Z Indexed on 2010/03/19 8:21 UTC
Read the original article Hit count: 177

Let's say we have two objects.

Furthermore, let's assume that they really have no reason to exist without each other. So we aren't too worried about re-usability.

Is there anything wrong with them "knowing about" each other? Meaning, can each one have the other as a property?

Is it OK to do something like this in a mythical third class:

Foo *f = [[Foo alloc] init];
self.foo = f;
[f release];

Bar *b = [[Bar alloc] init];
self.bar = b;
[b release];

foo.bar = bar;
bar.foo = foo;

...so that they can then call methods on each other? Instead of doing this, I'm usually using messaging, etc., but sometimes this seems like it might be a tidier solution.

I hardly ever see it in example code (maybe never), so I've shied away from doing it. Can somebody set me straight on this? Thanks.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about object-oriented-design