在mutableArray中添加新元素之后,添加的元素(从字典中获取)也将替换为新添加的元素。
for (int i = 0; i < [_ContentArray count]; i++){
if ([[[_ContentArray objectAtIndex:i]objectForKey:@"type"] isEqualToString:@"video"]) {
NSString *videoUrl = [[_ContentArray objectAtIndex:i]objectForKey:@"url"];
NSString *videoName = [[_ContentArray objectAtIndex:i]objectForKey:@"title"];
[_videoContentDict setValue:videoUrl forKey:@"url"];
[_videoContentDict setValue:videoName forKey:@"title"];
[_videoArray addObject:_videoContentDict];
NSLog(@"%@%%",_videoArray);
}
}
这里 - _videoContentDict是一个mutableDictionary _videoArray是一个mutableArray
答案 0 :(得分:1)
我认为每次将带有新对象的旧对象都带到NSMutableArray
。因为你在for循环之外被分配了NSMutableDictionary
。
所以将你的NSMutableDictionary
alloc init放在for循环中,如下面的代码:
for (int i = 0; i < [_ContentArray count]; i++){
if ([[[_ContentArray objectAtIndex:i]objectForKey:@"type"] isEqualToString:@"video"]) {
_videoContentDict = [[NSMutableDictionary alloc]init];
NSString *videoUrl = [[_ContentArray objectAtIndex:i]objectForKey:@"url"];
NSString *videoName = [[_ContentArray objectAtIndex:i]objectForKey:@"title"];
[_videoContentDict setValue:videoUrl forKey:@"url"];
[_videoContentDict setValue:videoName forKey:@"title"];
[_videoArray addObject:_videoContentDict];
NSLog(@"%@%%",_videoArray);
}
}