Re: Accessing objects two ways in an object hierarchy
Re: Accessing objects two ways in an object hierarchy
- Subject: Re: Accessing objects two ways in an object hierarchy
- From: Graham Cox <email@hidden>
- Date: Tue, 6 Jan 2009 21:01:53 +1100
On 5 Jan 2009, at 7:15 pm, Per Ohlson wrote:
I have a relationship similar to the following model in my program:
http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/Art/object_graph.gif
To access the objects corresponding to the collection of "employees"
from the "department", I just iterate through the collection and
fetch the object.
But what is the best way to access the "department" from an
"employee"?
There's no reason not to use a back-pointer. Just don't create a
retain cycle. In other words, decide who owns what - in this case,
'department' would own 'employees' I guess - so the reference back to
department would be weak, i.e. unretained.
I typically set back-pointers when objects are added to collections,
so I'd have a department method:
- (void) addEmployee:(Employee*) emp
{
[mEmployeesArray addObject:emp];
[emp setDepartment:self];
}
and an employee method:
- (void) seDepartment:(Department*) dept
{
mDeptRef = dept;
}
hth,
Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden