• iOS macOS NSArray,NSMutableArray, NSDictionary, NSMutableDictionary,增加,删除,修改,查找取值,基本操作


    不可修改的数组对象 NSArray

    创建方法:@[array]

    初始化

    //初始化
        NSArray *array = [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F", nil];
        NSArray *array1 = @[@"a",@"b",@"c",@"d"];
        NSArray *array2 = [[NSArray alloc]init];//一般不会这样用,显得太傻
        NSArray *array3 = @[];//一般要设置为空的数组也不会这样

    一般NSArray就是赋值和读取而已,还有清空。

    读取NSArray的值

    //获取数组元素个数
        NSLog(@"%lu",(unsigned long)array.count);//6
        //获取数组最后一个元素
        NSLog(@"%@",[array lastObject]);//F
        //获取数组第一个元素
        NSLog(@"%@",[array firstObject]);//A
        //获取对象C在数组的位置
        NSLog(@"%lu",(unsigned long)[array indexOfObject:@"C"]);//  2
         //获取数组中索引为2的对象
        NSLog(@"%@",[array objectAtIndex:2]);//C

    遍历读取

      //常规老方法
       for (int i = 0 ; i< array.count; i++) {
            NSLog(@"遍历-%@",array[i]);
        }
        
        //增强循坏方法
        for (NSString *str in array) {
            NSLog(@"遍历-%@",str);
        }
    
       //OC迭代器读取
        [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSLog(@"遍历-%@-%lu",obj,(unsigned long)idx);
        }];

    NSArray中的所有对象执行方法(我还没搞懂这个啥有啥用)

    @interface Student : NSObject
    - (void)log;
    - (void)logInfo:(NSString *)str;
    @end
    
    @implementation Student
    - (void)log{
        NSLog(@"打印");
    }
    - (void)logInfo:(NSString *)str{
        NSLog(@"打印-%@",str);
    }
    @end
    
    ---------------------
    NSArray *array3 = @[[Student new],[Student new],[Student new],[Student new]];
    [array3 makeObjectsPerformSelector:@selector(log)];
    [array3 makeObjectsPerformSelector:@selector(logInfo:) withObject:@"p"];

    NSArray排序(应该没什么大用)

     NSArray *arr = @[@"90",@"20",@"30",@"10",@"50",];
        //数组中的元素不可以是自定义的
        NSArray *arr1 = [arr sortedArrayUsingSelector:@selector(compare:)];
        NSLog(@"%@",arr1);
        /*
         10,20,30,50,90
         */
    
        //NSSortStable:串行排序   NSSortConcurrent:并发排序
        NSArray *arr2 = [arr sortedArrayWithOptions:NSSortStable usingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
            //每次调用
            NSLog(@"ob1--%@,obj2--%@",obj1,obj2);
            return [obj1 intValue] < [obj2 intValue];
        }];
        NSLog(@"%@",arr2);
         /*
         90,50,30,20,10
         */

    NSArray和NSString转换(这个很有用,非常有用!)

     NSArray *arr = @[@"A",@"B",@"C",@"D"];
        NSString *str = [arr componentsJoinedByString:@"-"];
        NSLog(@"%@",str);//A-B-C-D
    
        NSString *str1 = @"A-B-C-D";
        NSArray *arr1 = [str1 componentsSeparatedByString:@"-"];
        NSLog(@"%@",arr1);
        /*
         A,B,C,D
         */

    NSArray文件读写(这个还可以,少用)

     NSArray *array = [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F", nil];
        //将数组写入文件,本质是写入一个XML文件,iOS中一般写入plist,不能写入自定义对象
        BOOL flag = [array writeToFile:@"/Users/soso/Desktop/demo1/NSArray/demo.plist" atomically:YES];
        NSLog(@"%d",flag);
    
        //读文件
        NSArray *arr = [NSArray arrayWithContentsOfFile:@"/Users/soso/Desktop/demo1/NSArray/demo.plist"];
        NSLog(@"%@",arr);//A,B,C,D,E,F

    NSArray清空数据(这个经常用!)

    [NSArray removeAllObjects];

    可修改的数组对象 NSMutableArray

    创可变数组removeAllObjects引起崩溃

    self.showDataSource =(NSMutableArray *)dataArray;

    [self.showDataSource  removeAllObjects];

    解决方法:改成这样

    self.showDataSource = [[NSMutableArray alloc] initWithArray:dataArray];

    创建一个空的数组

    NSMutableArray *array = [NSMutableArray alloc] init];//这并不是一个好方法

    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10]; //创建一个可变的数组长度为10

    一、capacity概念

    NSMutableArray *orderIds = [NSMutableArray arrayWithCapacity:self.dataArray.count]


    初始化可变数组对象的长度,如果后面代码继续添加数组超过长度以后长度会自动扩充.

    初始化方法 capacity后的NSUInteger代表了开辟内存的一个单位 初始在内存中开辟5个内存,如果之后数组元素多余5个,则会再开辟新的52个新的内存,[考虑到数组的连续内存的特性] 单位是以5,把之前的5个元素的内容拷贝到新的十个新的内存里面,把第六个 也放进去,然后释放初始状态创建的内存5个 最后得到了一块够用的连续的内存52

     NSMutableArray *arr = [NSMutableArray array];
     NSMutableArray *arr1 = [];

    1.可变数组添加元素

     //添加元素
        [arr addObject:@"A"];
        NSLog(@"%@",arr);//A
    
    //插入一个元素
        [arr insertObject:@"D" atIndex:2];
        NSLog(@"%@",arr);// A,B,D,C
    
    //插入多个元素
        NSRange range = NSMakeRange(2, 2);
        NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:range];
        [arr insertObjects:@[@"H",@"G"] atIndexes:set];
        NSLog(@"%@",arr);//A,B,H,G,D,C
    
    //将指定数组中的元素都取出来,放到arrM中,拼接数组
        [arr addObjectsFromArray:@[@"B",@"C"]];
        NSLog(@"%@",arr);// A,B,C

    2.可变数组删除元素

     //删除
        [arr removeObject:@"B"];//删除其中一个元素
        [arr removeObjectAtIndex:2];//删除指定位置的元素
        [arr removeLastObject];//删除最后一个元素
        [arr removeAllObjects];//清空数组

    3.可变数组修改元素

     //替换
        [arr replaceObjectAtIndex:3 withObject:@"L"];
        NSLog(@"%@",arr);//A,B,H,L,D,C

    4.可变数组查找读取元素

    //常规老方法
       for (int i = 0 ; i< arr.count; i++) {
            NSLog(@"遍历-%@",array[i]);
        }
        
        //增强循坏方法
        for (NSString *str in arr) {
            NSLog(@"遍历-%@",str);
        }
    
       //OC迭代器读取
        [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSLog(@"遍历-%@-%lu",obj,(unsigned long)idx);
        }];

    不可修改的字典对象 NSDictionary

    创建方法: @{dictionary}

    创建一个空的字典

    //不要这样初始化,这是一个空的字典不可变字典一般直接赋值使用,常常用于一次性数据的读取,并且不可以做修改

    NSDictionary *dict = [NSDictionary dictionary];//不可取

    正确的使用

    方法一:

    NSDictionary *dict = [NSDictionary dictionaryWithObject:@"lnj" forKey:@"name"];
    //根据key获取value
    NSString *name = [dict objectForKey:@"name"];
    NSLog(@"name = %@", name);

    方法二:

    // 注意: key和value 是一一对应
    NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[@"lnj", @"30", @"1.75"] forKeys:@[@"name", @"age", @"height"]];
    NSLog(@"%@ %@ %@", [dict objectForKey:@"name"], [dict objectForKey:@"age"], [dict objectForKey:@"height"]);

    方法三:

    //简写:
    
    NSDictionary *dict = @{key:value};
    NSDictionary *dict = @{@"name": @"lnj"};
    NSLog(@"%@", dict[@"name"]);
    
    NSDictionary *dict = @{@"name":@"lnj", @"age":@"30", @"height":@"1.75"};
    NSLog(@"%@ %@ %@", dict[@"name"], dict[@"age"], dict[@"height"]);

    只有读取赋值的信息,和删除字典

    1.取值(遍历) 

    NSDictionary *dict = @{@"name":@"lnj", @"age":@"30", @"height":@"1.75"};
    
    //获取字典中key和value的个数, 在字典中key称之为键, value称之为值
    NSLog(@"count = %lu", [dict count]);

    方法一:老式for循环写法

    for (int i = 0; i < dict.count; ++i) {
        // 获取字典中所有的key
        NSArray *keys = [dict allKeys];
        // 取出当前位置对应的key
        //        NSLog(@"%@", keys[i]);
        NSString *key = keys[i];
        NSString *value = dict[key];
        NSLog(@"key = %@, value = %@", key, value);
    }

    方法二:增强for循环写法

    // 如何通过forin遍历字典, 会将所有的key赋值给前面的obj
    for (NSString *key in dict) {
        //        NSLog(@"%@", key);
        NSString *value = dict[key];
        NSLog(@"key = %@, value = %@", key, value);
    
    }

    方法三:OC字典的迭代器来遍历

    [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        NSLog(@"key = %@, value = %@", key, obj);
    }];

    清空NSDictionary字典

    //需要确定你使用的字典是可变的
    NSMutableDictionary *dictionary = [@{@"key":@"value"} mutableCopy];
    NSLog(@"消空前:\n%@",dictionary);
    [dictionary removeAllObjects];
    NSLog(@"消空后:\n%@",dictionary);

    ⚠️附加字典的文件操作

    如何对字典文件进行读写?

    1.将字典数据写入文件中

    NSDictionary *dict = @{@"name":@"lnj", @"age":@"30", @"height":@"1.75"};
    
    [dict writeToFile:@"/Users/xiaomage/Desktop/info.plist" atomically:YES];

    2.从文件中读取字典数据

    • 注意: 字典和数组不同, 字典中保存的数据是无序的
    • NSDictionary *newDict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/xiaomage/Desktop/info.plist"];
      NSLog(@"%@", newDict);
      
      NSArray *arr = @[@10, @20, @30, @5];
      [arr writeToFile:@"/Users/xiaomage/Desktop/abc.plist" atomically:YES];

    可修改的字典对象 NSMutableDictionary

    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];

    1.增加元素

    [dictM setObject:@"lnj" forKey:@"name"];
    //简写:
    dictM[@"name"] = @"lnj";

    2.删除元素

    [dictM removeObjectForKey:@"name"];
    [dictM removeObjectsForKeys:@[@"age", @"height"]];

    3.修改元素

    • 利用setObject方法给同名的key赋值, 那么新值会覆盖旧值
    • [dictM setObject:@"88" forKey:@"age"];
      //简写:
      dictM[@"age"] = @"88";

    4.查找取值读取

    NSLog(@"name = %@", dictM[@"name"]);
    注意⚠️
    1.不能使用@{}来创建一个可变的字典
     NSMutableDictionary *dictM = @{@"name":@"lnj"};//编译就会报错
     [dictM setObject:@"30" forKey:@"age"];

    2.

    • 如果是不可变字典, 那么key不能相同
    • 如果是不可变字典出现了同名的key, 那么后面的key对应的值不会被保存
    • 如果是在可变数组中, 后面的会覆盖前面的
    NSDictionary *dict = @{@"name":@"lmj", @"name":@"lnj"};
    NSLog(@"dict = %@", dict);
    
    NSMutableDictionary *dictM = [NSMutableDictionary dictionaryWithObjects:@[@"lmj", @"lnj"] forKeys:@[@"name", @"name"]];
    NSLog(@"dict = %@", dictM);
     
  • 相关阅读:
    名称空间与作用域
    3.19作业
    函数的参数
    文件的f.seek和文件修改方式以及函数的基本使用
    3.17作业
    文件处理
    3.16作业
    转 移动端-webkit-user-select:none导致input/textarea输入框无法输入
    移动端开发兼容性总结
    移动端input 无法获取焦点的问题
  • 原文地址:https://www.cnblogs.com/gaozhang12345/p/16312492.html
Copyright © 2020-2023  润新知