Skip to content

xmlElementDictionaryForKey / index #1

@bastianreich

Description

@bastianreich

When I use objectForKey:atIndex for a given key with e.g. atIndex=2 i want to get the
2nd occurance of exactly that key.

but, xmlElementDictionaryForKey compares the absolute index of the rootArray with
the relative index given by, let's say objectForKey:atIndex.

Suggestion:

-original-

- (NSMutableDictionary *)xmlElementDictionaryForKey:(NSString *)key atIndex:(NSInteger)index {
    for (NSInteger i = 0; i < self.rootArray.count; i++) {
        NSMutableDictionary *dictionary = [self.rootArray objectAtIndex:i];
        if ([dictionary objectForKey:key] && (index == NSIntegerMax)?YES:(i == index)) {
            return [dictionary objectForKey:key];
        }
    }
    return nil;
}

-modified-

- (NSMutableDictionary *)xmlElementDictionaryForKey:(NSString *)key atIndex:(NSInteger)index {
    int keyMatch = 0;
    for (NSInteger i = 0; i < self.rootArray.count; i++) {
        NSMutableDictionary *dictionary = [self.rootArray objectAtIndex:i];
        if ([dictionary objectForKey:key] && ((index == NSIntegerMax)?YES:(keyMatch == index))) {
            return [dictionary objectForKey:key];
        }

        if ([dictionary objectForKey:key]) {
            keyMatch++;
        }
    }
    return nil;
}

Best regards

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions