zoukankan      html  css  js  c++  java
  • iOS

    第一种:利用NSDictionary的AllKeys(AllValues)方法

    代码:

    NSArray *dataArray = @[@"2018-02-01",@"2018-02-02",@"2018-02-03",  
                               @"2018-02-01",@"2018-02-02",@"2018-02-03",  
                               @"2018-02-01",@"2018-02-03",@"2018-02-03",  
                               @"2018-02-01",@"2018-02-02",@"2018-02-03",  
                               @"2018-02-01",@"2018-02-02",@"2018-02-03",  
                               @"2018-02-01",@"2018-02-02",@"2018-02-03",  
                               @"2018-02-04",@"2018-02-06",@"2018-02-08",  
                               @"2018-02-05",@"2018-02-07",@"2018-02-09"];  
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithCapacity:0];  
        for(NSString *str in dataArray)  
        {  
            [dic setValue:str forKey:str];  
        }  
        NSLog(@"%@",[dic allKeys]);  

    打印结果:

    (
        "2018-02-09",
        "2018-02-02",
        "2018-02-08",
        "2018-02-01",
        "2018-02-07",
        "2018-02-06",
        "2018-02-05",
        "2018-02-04",
        "2018-02-03"
    )

    第二种方法:利用NSSet的AllObjects方法

    代码:

    NSArray *dataArray = @[@"2018-02-01",@"2018-02-02",@"2018-02-03",  
                               @"2018-02-01",@"2018-02-02",@"2018-02-03",  
                               @"2018-02-01",@"2018-02-03",@"2018-02-03",  
                               @"2018-02-01",@"2018-02-02",@"2018-02-03",  
                               @"2018-02-01",@"2018-02-02",@"2018-02-03",  
                               @"2018-02-01",@"2018-02-02",@"2018-02-03",  
                               @"2018-02-04",@"2018-02-06",@"2018-02-08",  
                               @"2018-02-05",@"2018-02-07",@"2018-02-09"];  
    NSSet *set = [NSSet setWithArray:dataArray]; 
     NSLog(@"%@",[set allObjects]);

    打印结果:

    (
        "2018-02-01",
        "2018-02-02",
        "2018-02-03",
        "2018-02-04",
        "2018-02-06",
        "2018-02-08",
        "2018-02-05",
        "2018-02-07",
        "2018-02-09"
    )

    第三种方法,利用数组的containsObject来去除

    代码:

    NSArray *dataArray = @[@"2018-02-01",@"2018-02-02",@"2018-02-03",  
                               @"2018-02-01",@"2018-02-02",@"2018-02-03",  
                               @"2018-02-01",@"2018-02-03",@"2018-02-03",  
                               @"2018-02-01",@"2018-02-02",@"2018-02-03",  
                               @"2018-02-01",@"2018-02-02",@"2018-02-03",  
                               @"2018-02-01",@"2018-02-02",@"2018-02-03",  
                               @"2018-02-04",@"2018-02-06",@"2018-02-08",  
                               @"2018-02-05",@"2018-02-07",@"2018-02-09"];  
    NSMutableArray *listAry = [[NSMutableArray alloc]init];
    for (NSString *str in dataArray) {
    if (![listAry containsObject:str]) {
    [listAry addObject:str];
    }
    }
    NSLog(@"%@",listAry);

    打印结果:

     
    
     (
        "2018-02-01",
        "2018-02-02",
        "2018-02-03",
        "2018-02-04",
        "2018-02-06",
        "2018-02-08",
        "2018-02-05",
        "2018-02-07",
        "2018-02-09"
    )

    第四种方法:利用keyValue的方式

    代码:

     
    
    NSArray *dataArray = @[@"2018-02-01",@"2018-02-02",@"2018-02-03",  
                               @"2018-02-01",@"2018-02-02",@"2018-02-03",  
                               @"2018-02-01",@"2018-02-03",@"2018-02-03",  
                               @"2018-02-01",@"2018-02-02",@"2018-02-03",  
                               @"2018-02-01",@"2018-02-02",@"2018-02-03",  
                               @"2018-02-01",@"2018-02-02",@"2018-02-03",  
                               @"2018-02-04",@"2018-02-06",@"2018-02-08",  
                               @"2018-02-05",@"2018-02-07",@"2018-02-09"];  
    dataArray = [dataArray valueForKeyPath:@"@distinctUnionOfObjects.self"];
    NSLog(@"%@",dataArray);

    打印结果如下:

    (  
        "2018-02-01",  
        "2018-02-02",  
        "2018-02-03",  
        "2018-02-04",  
        "2018-02-06",  
        "2018-02-08",  
        "2018-02-05",  
        "2018-02-07",  
        "2018-02-09"  
    )  

    总结:这几种方式都可以实现去除重复的数据,当然也可以用for循环的方式去除方法不再叙述;参考了别人的东西进行的总结

    案例的使用:

    服务器返回一个带有日期的数组,当然有重复的日期,同一个的日期的列表要在同一个地方的话,头部显示日期的话,可以使用到此方法去除重复的,当然,根据具体的情况代码和逻辑是不同的!

  • 相关阅读:
    delphi参数传递
    Delphi OO
    Delphi Excel
    Delphi Register
    西安前端交流会
    web前端开发教程系列-4
    web前端开发教程系列-3
    web前端开发教程系列-2
    web前端开发教程系列-1
    露个脸
  • 原文地址:https://www.cnblogs.com/gongyuhonglou/p/8409412.html
Copyright © 2011-2022 走看看