2013-08-04 54 views
0

我已经尽了全力来搜索答案的NSMutableDictionary添加到NSMutableArray里。不幸的是,他们都要求我使用“[allValues]”,这不是我想要的。只有的NSMutableDictionary的最后的值被添加到NSMutableArray的

这里是问题,

在头文件中,我定义,

NSMutableArray *arData; 

而在M档,

arData = [[NSMutableArray alloc] init]; 

NSMutableDictionary *mutableDictionary5 = [[NSMutableDictionary alloc]init]; 

for (i=0;i<[[[self rssParser]rssItems]count];i++) 
{ 

    Lat = [[[[[self rssParser]rssItems]objectAtIndex:i]lat] doubleValue]; 
    Lng = [[[[[self rssParser]rssItems]objectAtIndex:i]lng] doubleValue]; 
    // Creates a marker in the center of the map. 
    GMSMarker *marker = [[GMSMarker alloc] init]; 
    marker.position = CLLocationCoordinate2DMake(Lat, Lng); 
    marker.title = [[[[self rssParser]rssItems]objectAtIndex:i]header]; 

[mutableDictionary5 setObject:marker.title forKey:@"title"]; 

[arData setValue:[mutableDictionary5 valueForKey:@"title"] forKey:@"title"]; 
} 

的 “arData” 只得到了最后一个值的[[[[自我rssParser]的RSSItems] objectAtIndex:我]头]。而我肯定mutableDictionary5得到,因为我已经把下面的代码末尾的所有价值,

NSLog(@"mutableDictionary5 values=%@",[mutableDictionary5 allValues]); 

它打印出标题的所有值。

我的理解不应该是setValue方法,但是,我试图用[mutableDictionary5 allValues], “addobjects”, “allkeys”。他们不工作。我该如何让NSMutableArray arData以“title”作为关键字添加标题的所有值?谢谢。

对不起我的英语不好,这不是我的母语。

+0

简单地使用'[arData addObject:[mutableDictionary5 objectForKey:@“title”]];' –

+0

它给出了字符串响应,并在给objectforkey传递数据时给出错误 –

回答

1

您错误地使用setValue代替addObject

[arData addObject:mutableDictionary5]; 

,如果你的Alloc这将工作初始化一个新的字典里面循环。
如果你想减少阵列只是标题做到这一点后的循环:

NSArray *titleArray = [arData valueForKeyPath:@"title"]; 

现在你有一个仅包含标题的数组。

顺便说一句,你可以做出一些努力来创造更好的变量名称。请注意,通过Objective-C中的约定,变量名称以低限值开始,而类名称为大写。

+0

我想我需要为此创建多个nsmutableDictionary 。非常感谢。它现在有效! – babyghost

相关问题