[英]NSDictionary within a NSDictionary
At the moment i've got the following code to get data from an xml file. 目前我有以下代码从xml文件中获取数据。 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 ([elementName 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;
detailview可以检查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 Thnx家伙
Yes, this is a good idea. 是的,这是一个好主意。 Just pass the 'inner' dictionary as the object parameter to
setObject:forKey:
on the 'outer' dictionary. 只需将'inner'字典作为对象参数传递给'外'字典上的
setObject:forKey:
. It'll just work. 它会起作用。 For example:
例如:
NSMutableDictionary *outer=[[NSMutableDictionary alloc] init];
NSDictionary *inner=[NSDictionary dictionaryWithObject:@"hello" forKey:@"world"];
[outer setObject:inner forKey:@"greetings"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.