Re: NSEnumerator doesn't support NSCopying - why?
Re: NSEnumerator doesn't support NSCopying - why?
- Subject: Re: NSEnumerator doesn't support NSCopying - why?
- From: Allan Odgaard <email@hidden>
- Date: Mon, 16 Feb 2004 16:42:16 +0100
On 10. Feb 2004, at 11:09, Wade Tregaskis wrote:
Now, that'd be fantastic if NSEnumerator supported NSCopying. As is
it raises an NSInvalidArgumentException (originating in
copyWithZone:).
It is not declared to supprt NSCopying, maybe just an oversight.
I'm hesitant to revert to indexing into the array, since I would hope
that using an enumerator would be faster.
I doubt that it is -- I think NSArray is just a wrapper for CFArray
(hence the toll-free bridging), and CFArray does not support
enumeration (AFAIK), so the NSEnumerator returned from NSArray would
most likely just index the array.
It's probably easy for you to write the indexing yourself, but let me
anyway recommend my CocoaSTL:
http://www.top-house.dk/~aae0030/cocoastl/ -- it gives STL conforming
iterators for Cocoa classes, and these do support copying.
So to check if the array has two equal elements, the code would be:
NSArray* array = ...;
foreach(outer, beginof(array), endof(array))
{
foreach(inner, outer+1, endof(array))
{
if(*inner == *outer)
cout << *inner << " is equal to " << *outer << endl;
}
}
Here the array iterator actually returns a smart pointer for the
ObjectiveC object where operator== is overloaded to use isEqual:.
btw: if the objects suppoert compare: (i.e. has an ordering) a faster
way would be to sort the elements and do a linear check, e.g.:
NSMutableArray* array = ...;
std::sort(beginof(array), endof(array));
foreach(it, beginof(array), endof(array))
{
it = std::adjacent_find(it, endof(array));
if(it == endof(array))
break;
cout << *it << " appears more than once in the array" << endl;
}
Kind regards Allan
_______________________________________________
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.