0

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;
}
ipmcc
  • 29,581
  • 5
  • 84
  • 147
Avba
  • 14,822
  • 20
  • 92
  • 192

1 Answers1

2

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.

ipmcc
  • 29,581
  • 5
  • 84
  • 147