Re: dealloc and instance variables
Re: dealloc and instance variables
- Subject: Re: dealloc and instance variables
- From: Jörn Salewski <email@hidden>
- Date: Sun, 23 Nov 2003 11:24:20 +0100
am 23.11.2003 9:55 Uhr schrieb Graeme Mathieson unter email@hidden:
I'm no expert myself. I can not give decicive answeres, but only a few
thoughts.
>
> // if sString is an immutable NSString
>
> // a simple retain is sufficient.
>
>
That brings up another thing I'm not entirely sure about. Surely
>
somebody could in fact pass in an NSMutableString and, despite the fact
>
my object thinks its immutable, merrily mess around with my object's
>
data without going through the appropriate accessors? I guess the only
>
way to be sure would be something along the lines of:
>
>
- (id)initWithString:(NSString *)aString
>
{
>
if((self = [super init])) {
>
if([aString isMemberOfClass:[NSString class]]) {
>
baz = [aString retain];
>
} else {
>
baz = [aString copy];
>
}
>
}
>
return self;
>
}
>
>
Or is that being too paranoid?
Somewhere you have to start to make certain assumptions. Despite of compiler
warnings, one could pass any object to the initWithString: method. What
happens if passing in an instance of class XYZ that doesn't implement a copy
method? Checking this would start becoming extremely paranoid, I think.
The question just comes into my mind, if the above code actually works as
you expect. Since NSString is a class cluster, an immutable NSString passed
on to the init method doesn't actually need to be a concrete subclass of
NSString. Chances are great that it will be an instance of NSCFString for
example.
So, maybe it's best (and easiest) to leave it as it was simply with the copy
not doing any typechecking... I don't know.
>
Likewise, if my class internally uses
>
an NSMutableArray to store some data, and I would like an accessor
>
which gives (read-only) access to that array, is it sufficient to do:
>
>
@interface FooBar
>
{
>
NSMutableArray *attributes;
>
}
>
- (NSArray *)attributes;
>
@end
>
[ ... ]
>
- (NSArray *)attributes
>
{
>
return attributes;
>
}
>
>
or should I do:
>
>
- (NSArray *)attributes
>
{
>
return [NSArray arrayWithArray:attributes];
>
}
>
I think it depends. In my code I often use the first variant, especially if
the array only needs to be mutable during its creation, say in the init
method of the class. Usually one wouldn't try to alter an array retrieved
via such an accessor method somewhere else in the code, because one would
assume it's immutable (the compiler would also give a warning).
But, if its content is going to be changed later on, variant to is the way
to go for. Imagine the following scenario:
Someone is calling [fooBar attributes] and iterates through ist content with
an enumerator. While other code going through the array fooBar decides to
remove some elements. The programm will very likely start to misbehave.
Yours,
Joern Salewski
_______________________________________________
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.