美文网首页
object-c 之 NSMutableArray

object-c 之 NSMutableArray

作者: 墨凌风起 | 来源:发表于2016-12-29 09:45 被阅读61次
    //1 创建指定容量大小的可变数组对象  开始时候申请的内存大小 可以根据以后情况进行扩展
            NSMutableArray *array = [[NSMutableArray alloc]initWithCapacity:20];
    
     //2 添加数组元素
            [array addObject:@"one"];
            NSLog(@"array = %@",array);
    
          //3 在指定下标处 插入元素
            [array insertObject:@"two" atIndex:1];
            NSLog(@"array  =%@",array);
    
            //4 添加传入数组的所有元素到数组中
            [array addObjectsFromArray:array];
             NSLog(@"array = %@",array);
    
            //5 删除最后一个元素
            [array removeLastObject];
            NSLog(@"array = %@",array);
    
            //6  删除指定位置
            [array removeObjectAtIndex:0];
             NSLog(@"array = %@",array);
            NSMutableArray * array1 = [NSMutableArray arrayWithObjects:@"one",@"two",@"three",@"four",@"five",@"one", nil];
    
            //7 删除数组中所有出现的目标元素
            [array1 removeObject:@"one"];
            NSLog(@"array1 = %@",array1);
    
           // 8  删除某个范围内 目标元素
            [array1 removeObject:@"one" inRange:NSMakeRange(4, 2)];
            NSLog(@"array1 = %@",array1);
    
            //9  删除数组中某个范围
            [array1 removeObjectsInRange:NSMakeRange(0, 3)];
            NSLog(@"array1 = %@",array1);
            //10 删除 传入输出数组在数组中的元素
            [array1 removeObjectsInArray:array];
            NSLog(@"array1 = %@",array1);
    
            //11 删除所有元素
            [array1 removeAllObjects];
            NSLog(@"array1 = %@",array1);
    
            //12 修改或重置数组
            [array setArray:array];
            NSLog(@"array = %@",array);
    
            //13 交换指定位置的元素
            [array1 exchangeObjectAtIndex:0 withObjectAtIndex:1];
            NSLog(@"array1 = %@",array1);
    
            //一个数组替换一个范围
            [array1 replaceObjectsInRange:NSMakeRange(1, 2) withObjectsFromArray:@[@"ni",@"hao"]];
            NSLog(@"array1 = %@",array1);
    
            //使用传入元素 替换某个下标的元素
           [array1 replaceObjectAtIndex:1 withObject:@"one"];
            NSLog(@"array1 = %@",array1);
            
    
            //下标集合
            NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSetWithIndex:1];
            [indexSet addIndex:3];
            [indexSet addIndex:5];
            
            [array1 insertObjects:@[@"1",@"3",@"5"] atIndexes:indexSet];
            NSLog(@"array1 = %@",array1);
            
            [array1 removeObjectsAtIndexes:indexSet];
            NSLog(@"array1 = %@",array1);
            
    

    注意:可变数组在for - in循环是不可以改变器内部元素,如果要改变,用枚举器

    //将sectionTitles中数字字符串换成汉字字符串,按顺序

    NSDictionary *regionDicts = @{@"19764":@"美洲",@"20954":@"韩日",@"20691":@"香港",@"21062":@"台湾",@"19774":@"中国",@"20681":@"欧洲"};
            NSMutableArray* sectionTitles = [NSMutableArray arrayWithObjects:@"20954",@"19764",@"20681",@"20691",@"21062",@"19774", nil];
            NSArray *regionAllKey = [regionDicts allKeys];
            
            for (NSString *regionKey in regionAllKey) {
                
                
                [sectionTitles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if ([obj isEqualToString:regionKey]) {
                        *stop = YES;
                        if (*stop) {
                            [sectionTitles replaceObjectAtIndex:idx withObject:[regionDicts objectForKey:regionKey]];
                        }
                   }
            }
            NSLog(@"调换后的sectionTitles = %@",sectionTitles);   
    

    相关文章

      网友评论

          本文标题:object-c 之 NSMutableArray

          本文链接:https://www.haomeiwen.com/subject/ivvuvttx.html