Re: NSArray/MutableArray subclass/extension
Re: NSArray/MutableArray subclass/extension
- Subject: Re: NSArray/MutableArray subclass/extension
- From: Ben Dougall <email@hidden>
- Date: Mon, 20 Oct 2003 20:27:10 +0100
On Monday, October 20, 2003, at 03:05 pm, Jonathan E. Jackel wrote:
I'd write a category on NSArray and include your new initWith...
method there. That way the method would be available to all NSArrays
and NSMutableArrays. You can call the built-in init methods from
your
method, and return the init-ed objects from your method.
thanks for the reply. yes i did try that but couldn't get it to work.
i
had problems with warnings about NSArray not responding to the various
extra NSMutableArray methods - but they were just warnings so maybe
they were fine left. i don't know.
You can test for whether you are dealing with a mutable or immutable
array
with:
if([self isKindOfClass:[NSMutableArray class]])
and you can also test for whether an object responds to a particular
selector with:
if([self respondsToSelector:@selector(initWithObjects:)])
To quiet the warnings, cast self to NSMutableArray once you are sure
that's
what you are dealing with.
i ended up making everything the
NSMutableArray type which got rid of the warnings but also got rid of
any possibility of an NSArray.
See above. Your NSArray category/method can treat NSArrays one way and
NSMutableArrays another way.
Another way to handle it would be to write two categories -- one on
NSArray
and one on NSMutableArray. You could have the same method name in both
categories, but a mutable array would use the mutable version, and vice
versa.
also i had a problem with when i was
populating the array in the extra init method i got "-count sent to an
uninitialized mutable array object" so there was a problem with
attempting to send messages to an array from within the init method.
I can't say exactly what you're doing wrong without seeing the code,
but the
error suggests you ought to call one of the NS(Mutable)Array's init...
methods before doing additional set-up work on the array in your
method.
started it again, still using one category, and, success :) i had to
use one (NSMutableArray*)cast, which is fine because it couldn't be a
NSArray at that point. so that's great. thanks very much for the info.
ben.
_______________________________________________
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.