Custom NSFastEnumeration method?

157 Views Asked by At

I Have a container class which stores its data in a dictionary

I would like to enumerate the objects and not the keys.

right now I have code like this

-(NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(__unsafe_unretained id [])buffer count:(NSUInteger)len
{
    return [self.container countByEnumeratingWithState:state objects:buffer count:len]; // this isn't working as container is dictionary
}

-(myobject *)objectAtIndex:(int)number // this is the method to retrieve a specific object
{
    int i = 0;
    for(id key in self.container) {
        if (number == i) {
            id value = [self.container objectForKey:key];
            return value;
        }
        else i++;
    }
    return nil;
}
1

There are 1 best solutions below

0
On

Why not just use the -allValues property like this:

-(NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(__unsafe_unretained id [])buffer count:(NSUInteger)len
{
    return [self.container.allValues countByEnumeratingWithState:state objects:buffer count:len];
}

Additionally, NSDictionary is an un-ordered container, so your -objectAtIndex: method makes no sense.