Re: Confused with Chapter 7 in Hillegass' book (2nd ed.)
Re: Confused with Chapter 7 in Hillegass' book (2nd ed.)
- Subject: Re: Confused with Chapter 7 in Hillegass' book (2nd ed.)
- From: mmalcolm crawford <email@hidden>
- Date: Wed, 28 Jul 2004 18:03:11 -0700
On Jul 28, 2004, at 5:32 PM, mmalcolm crawford wrote:
For many people, however, this smacks of breaking encapsulation, or
they want to put custom logic into their accessor methods, in which
case you might implement:
- (NSMutableArray *)employees
{
return employees;
}
and
- (void)setEmployees:(NSArray *)newEmployees
{
[employees autorelease];
employees = [newEmployees copy];
}
Omitted: "or some variant thereof."
Addendum:
Note also that since -employees is defined to return NSMutableArray*,
the set method should probably be more like:
- (void)setEmployees:(NSArray *)newEmployees
{
if (employees != newEmployees)
{
[employees autorelease];
employees = [newEmployees mutableCopy]; // this is the important line
}
}
There are a number of variants of this, see for example:
<
http://www.stepwise.com/Articles/Technical/2002-06-11.01.html>
Finally note that typically, to promote encapsulation, your get method
should return an immutable array:
- (NSArray *)employees
{
return employees;
}
(whether or not the array is itself mutable).
mmalc
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.