• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSEnumerator and the missing peek method
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSEnumerator and the missing peek method


  • Subject: Re: NSEnumerator and the missing peek method
  • From: Marcel Weiher <email@hidden>
  • Date: Fri, 30 Sep 2005 00:20:34 +0100




On 29 Sep 2005, at 22:27, Jan Vereecken wrote:


That is the solution I use at the moment. I was only wondering if someone maybe found a way to extend NSEnumerator with a new method to peek which would make following code possible.


while(myObject = [enumerator nextObject]) {
    do something (for example append a string)
    if([enumerator hasNext]) {
        do something (for example add a newline)
    }
}


Something like the following might do the trick (composed entirely in Mail, therefore probably not even remotely compilable):


@interface PeekingEnumerator : NSEnumerator
    NSEnumerator *sourceEnumerator;
    id    peekedObject;
}

-initWithSourceEnumerator:(NSEnumerator*)anEnumerator;
-(BOOL)hasNextObject;
-peek;

@end


@interface NSEnumerator(makePeekable)

-(PeekingEnumerator*)peekable;

@end



@implementation PeekingEnumerator

-initWithSourceEnumerator:(NSEnumerator*)anEnumerator
{
    self = [super init];
    [self setSourceEnumerator:anEnumerator];
    return self;
}

idAccessor( peekedObject, setPeekedObject )
objectAccessor( NSEnumerator* , sourceEnumerator, setSourceEnumerator )

-initWithSourceEnumerator

-sourceNextObject
{
    return [sourceEnumerator nextObject];
}

-peek
{
    if ( ![self peekedObject] ) {
        [self setPeekedObject:[self sourceNextObject]];
    }
    return [self peekedObject];
}

-nextObject
{
    id returnValue = [[[self peek] retain autorelease];
    [self setPeeked:nil];
    return returnValue;

}

-hasNextObject
{
    return [self peek] != nil;
}

-(void)dealloc
{
    [peekedObject release];
    [sourceEnumerator release];
    [super dealloc];
}

-(PeekingEnumerator*)peekable
{
    return self;
}

@end

@implementation NSEnumerator(makePeekable)

-(PeekingEnumerator*)peekable
{
return [[[PeekableEnumerator alloc] initWithSourceEnumerator:self] autorelease];
}


@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


References: 
 >NSEnumerator and the missing peek method (From: Jan Vereecken <email@hidden>)
 >Re: NSEnumerator and the missing peek method (From: Nick Zitzmann <email@hidden>)
 >Re: NSEnumerator and the missing peek method (From: Jan Vereecken <email@hidden>)

  • Prev by Date: Re: QTMovieView and NSViewAnimation
  • Next by Date: Re: NSError and user-cancel induced "errors"
  • Previous by thread: Re: NSEnumerator and the missing peek method
  • Next by thread: Re: NSEnumerator and the missing peek method
  • Index(es):
    • Date
    • Thread