Re: NSArray operator implementing
Re: NSArray operator implementing
- Subject: Re: NSArray operator implementing
- From: mmalcolm crawford <email@hidden>
- Date: Sat, 27 Nov 2004 11:52:29 -0800
On Nov 27, 2004, at 9:25 AM, Cornelius Jaeger wrote:
so NSArray receive's an @ in it's valueForKeyPath and tries to find an
internal method to resolve it? no need to register an observer?
Correct. Although I'm not sure if this is officially supported at the
moment. I know there was a discussion about it somewhere -- Scott?
thx for the great code.
Cheers, although looking at it again, and doing a quick experiment,
it's not as efficient as it might be. If you're using this as a
keypath it might get called often... The following implementation is a
little more complicated, but seems (after a very quick test, YMMV) to
be at least three times faster (sometimes much more than that).
[ Which is more elegant, recursion or polymorphism? -- you decide!
:-) ]
mmalc
@implementation NSArray (Flattened)
- (NSArray *)flattened
{
SEL aSelector = @selector(addToArray:);
NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self
count]];
[self makeObjectsPerformSelector:aSelector withObject:array];
return array;
}
- (void)addToArray:(NSMutableArray *)array
{
SEL aSelector = @selector(addToArray:);
[self makeObjectsPerformSelector:aSelector withObject:array];
}
@end
@implementation NSObject (Flattened)
- (void)addToArray:(NSMutableArray *)array
{
[array addObject:self];
}
@end
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden