gpt4 book ai didi

ios - nsmutableArray 用最后添加的对象替换元素

转载 作者:行者123 更新时间:2023-11-29 00:57:05 26 4
gpt4 key购买 nike

在 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 是一个可变数组

最佳答案

我想你每次都将旧对象和新对象放入 NSMutableArray 中。因为您在 for 循环之外分配了 NSMutableDictionary

所以让你的 NSMutableDictionary 像下面的代码一样在 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);

}

}

关于ios - nsmutableArray 用最后添加的对象替换元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37519219/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com