开发者

NSDictionary within a NSDictionary

开发者 https://www.devze.com 2023-02-19 09:54 出处:网络
At the moment i\'ve got the following code to get data from an xml file. This works so far - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName

At the moment i've got the following code to get data from an xml file. This works so far

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    attributes:(NSDictionary *)attributeDict{ 
    currentElement = [elementName copy];
    if ([elementName isEqualToString:@"Placemark"]) {
        placemarkData = [[NSMutableDictionary alloc] init];
        currentTitle = [[NSMutableString alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if ([currentElement isEqualToString:@"name"])
        [placemarkData addObject:string forKey:currentElement];
    if ([currentElement isEqualToString:@"address"])
        [placemarkData addObject:string forKey:currentElement];
    if ([currentElement isEqualToString:@"coordinates"])
        [placemarkData addObject:string forKey:currentElement];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName{ 
    if ([ele开发者_开发百科mentName isEqualToString:@"Placemark"]) {
        [Placemarks addObject:[placemarkData copy]];
        NSString *batsen = [placemarkData objectForKey:@"name"];
        NSLog(@"adding story: %@", batsen);
    }
}

This dictionary will be loaded into a array. Problem is that where this array got loaded in need to find the right entry in the array. I do it like this

 TabbedCalculationAppDelegate *appDelegate = (TabbedCalculationAppDelegate *)[[UIApplication sharedApplication] delegate];

    NSArray *jean = appDelegate.Placemarks;
    NSDictionary *boekje = [jean objectAtIndex:1];
    NSString *coordinaten = [boekje objectForKey:@"coordinates"];
    NSString *naam = [boekje objectForKey:@"name"];
    NSLog(@"poep: %@" , naam);
    NSLog(@"wwhoppa: %@", coordinaten);
    titel.text = coordinaten;

But since arrays works with index I can't find a way to get the right entry. To clarify, a user clicks in a mapview on an annotation. This annotation then gets the corresponding detailview. The detailview can check which annotation is clicked by the view.annotation.title;

So I actually need a dictionary in a dictionary with name as key in the root dictionary. Is this possible? and on what way I can implement this?

Is this a little bit clear? Else just ask!

Thnx guys


Yes, this is a good idea. Just pass the 'inner' dictionary as the object parameter to setObject:forKey: on the 'outer' dictionary. It'll just work. For example:

NSMutableDictionary *outer=[[NSMutableDictionary alloc] init];
NSDictionary *inner=[NSDictionary dictionaryWithObject:@"hello" forKey:@"world"];
[outer setObject:inner forKey:@"greetings"];
0

精彩评论

暂无评论...
验证码 换一张
取 消