Objective-C Objects Having Each Other as Properties
- by mwt
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.