数组删除和用遍历修改数据或复杂数据

时间:2022-03-09 19:14:02

首先我们来说下简单点的数据,上例子吧

一、简单数组或字典的元素替换及删除

//数组元素的替换和删除
NSMutableArray *Arr = [NSMutableArray arrayWithObjects:@"123",@"abc",@"ABC", nil];
//这里我们将123换成456吧
[Arr replaceObjectAtIndex:0 withObject: @"456"];

[array removeAllObject] 删除所有
[array removeObjectAtIndex:index]按下标删除元素

//字典的替换元素和删除
NSMutableDictionary *itemDic = [[NSMutableDictionary alloc] init];//创建一个可变字典
[itemDic setValue:@"123" forKey:@"A"];//添加一组键值(注:这个方法也可用于替换元素)
[itemDic setValue:@"456" forKey:@"A"];//替换元素,如此,123就被456替换了

//删除字典
//通过指定key删除
[mutableDic2 removeObjectForKey:@"k4"];
NSLog(@"dic2:%@",mutableDic2);

//删除多个键值
NSArray *keys=[NSArray arrayWithObjects:@"k1",@"k2",@"k3", nil];
[mutableDic2 removeObjectsForKeys:keys];
NSLog(@"dic2:%@",mutableDic2);

//删除全部键值
[mutableDic2 removeAllObjects];
NSLog(@"dic2:%@",mutableDic2);


二、复杂数组或字典的替换与删除

:复杂数组或字典就是数组和字典相嵌套的数据,这种数据想要改变其中的某个值,就要用到遍历或者枚举,这里我们讲解一下怎么用遍历来实现复杂数据的元素替换和删除

<p class="p1">//元素替换<span class="s2"></span></p><p class="p1"><span class="s2" style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);"><span class="s1" style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);">NSMutableArray</span><span class="s2" style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);"> *itemArray = [</span><span class="s1" style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);">NSMutableArray</span><span class="s2" style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);"> </span><span class="s3" style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);">array</span><span class="s2" style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);">];</span>
</span></p><span class="s1" style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);"></span><pre name="code" class="objc"><span class="s1">NSMutableArray</span><span class="s2"> *Arr = [</span><span class="s1">NSMutableArray</span><span class="s2"> </span><span class="s3">arrayWithObjects</span><span class="s2">:</span><span class="s4">@{@"123":@[@"000",@"111"],@"abc":@[@"aaa",@"bbb"]}</span><span class="s2">, </span><span class="s5">nil</span><span class="s2">];</span>
<pre name="code" class="objc">//这里我们将Arr数组中的下标为0的字典键123对应的value中的第一条数据,也就是000改成888

for (NSDictionary *dic in Arr){ NSLog(@"dic:%@",dic); NSMutableDictionary *itemDic = [[NSMutableDictionary alloc] initWithDictionary:dic]; if (dic == 
Arr 
[0]) 

{ for (NSString *str in dic) { if ( [str isEqualToString : @"123" ] )
<span style="white-space:pre"></span> {
<pre name="code" class="objc"><pre name="code" class="objc"><span style="white-space:pre"></span>    NSLog(@"str:%@",str);
NSMutableArray *itemArr = [NSMutableArray arrayWithArray:dic[str]];
 
            [itemArr replaceObjectAtIndex:0 withObject: @"888"];
            [itemDic setValue:itemArr forKey:str];
            NSLog(@"itemDic:%@",itemDic);
 
<span style="white-space:pre"></span> }
} [itemArray addObject:itemDic]; } else { [itemArray addObject:dic]; }}

NSLog(@"itemArray:%@",itemArray);//这样就结束了,将000改成了888


//删除某个数据

<span class="s1">NSMutableArray</span><span class="s2"> *Arr = [</span><span class="s1">NSMutableArray</span><span class="s2"> </span><span class="s3">arrayWithObjects</span><span class="s2">:</span><span class="s4">@{@"123":@[@"000",@"111"],@"abc":@[@"aaa",@"bbb"]}</span><span class="s2">, </span><span class="s5">nil</span><span class="s2">];</span>

NSMutableArray *itemArray = [NSMutableArrayarray];

for (NSDictionary *dicin Arr)

{

NSLog(@"dic:%@",dic);

NSMutableDictionary *itemDic = [[NSMutableDictionaryalloc] initWithDictionary:dic];

if (dic ==Arr[0])

{

  for (NSString *strin dic)

                {

NSLog(@"str:%@",str);

NSMutableArray *itemArr = [NSMutableArrayarrayWithArray:dic[str]];

                        if ([strisEqualToString:@"123"])

                        {

[itemArr removeObjectAtIndex:0];

                        }

[itemDicsetValue:itemArrforKey:str];

}

[itemArray addObject:itemDic];

}

else

{

[itemArray addObject:dic];

}

}
 

NSLog(@"itemArray:%@",itemArray);//这样就结束了,将000删除了



三、利用遍历改变复杂数据的元素替换和删除就这么多代码,其实也不难,只要会了逻辑,写这个其实还是可以的,有什么不懂得,复制代码出现问题的,请留言、评论!谢谢!