Пользовательский метод NSFastEnumeration?

У меня есть контейнерный класс, который хранит свои данные в словаре

Я хотел бы перечислить объекты, а не ключи.

сейчас у меня есть такой код

-(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 ответ

Почему бы просто не использовать -allValues свойство как это:

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

Дополнительно, NSDictionary это не заказанный контейнер, поэтому ваш -objectAtIndex: метод не имеет смысла.

Другие вопросы по тегам