2016-01-27 12:01:21 sevenquan 阅读数 6820

- (void)viewDidLoad {

    [super viewDidLoad];

    

    

    

    //统计数组相同元素的个数

    

    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];

    

    //需要统计的数组

    NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"2",@"3",@"3",@"3", nil];

    NSSet *set = [NSSet setWithArray:array];

    

    for (NSString *setstring in set) {

        

        

        //需要去掉的元素数组

        NSMutableArray *filteredArray = [[NSMutableArray alloc]initWithObjects:setstring, nil];

        

        NSMutableArray *dataArray = array;

        

        NSPredicate * filterPredicate = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)",filteredArray];

        //过滤数组

        NSArray * reslutFilteredArray = [dataArray filteredArrayUsingPredicate:filterPredicate];

        NSLog(@"Reslut Filtered Array = %@",reslutFilteredArray);

        

        int number = (int)(dataArray.count-reslutFilteredArray.count);

        NSLog(@"number :%d",number);

        

        [dic setObject:[NSString stringWithFormat:@"%d",number] forKey:setstring];

    }

    

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

  


    //---------------------------------------------------------------------------------------

//参考数组筛选:

    

    //需要去掉的元素数组

    NSMutableArray *filteredArray = [[NSMutableArray alloc]initWithObjects:@"1",@"4", nil];

    //需要被筛选的数组

    NSMutableArray *dataArray = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"1",@"4",@"6",@"1",@"1",@"4",@"1",@"6",@"4", nil];

    

    /*

     方法一:利用NSPredicate

     注:NSPredicate所属Cocoa框架,在密码、用户名等正则判断中经常用到。

     类似于SQL语句

     NOT 不是

     SELF 代表字符串本身

     IN 范围运算符

     那么NOT (SELF IN %@) 意思就是:不是这里所指定的字符串的值

     */

    NSPredicate * filterPredicate = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)",filteredArray];

    //过滤数组

    NSArray * reslutFilteredArray = [dataArray filteredArrayUsingPredicate:filterPredicate];

    NSLog(@"Reslut Filtered Array = %@",reslutFilteredArray);

    /*

     结果:

     Reslut Filtered Array = (

     2,

     6,

     6

     )

     */

    

    /*

     方法二:从后往前遍历数组,然后匹配删除

     */

    int i = (int)[dataArray count]-1;

    for(;i >= 0;i --){

        //containsObject 判断元素是否存在于数组中(根据两者的内存地址判断,相同:YES  不同:NO

        if([filteredArray containsObject:[dataArray objectAtIndex:i]]) {

            [dataArray removeObjectAtIndex:i];

        }

    }

    NSLog(@"Data Array = %@",dataArray);

    /* 

     结果: 

     Data Array = ( 

     2, 

     6, 

     6 

     ) 

     */

    

}


2017-02-23 18:23:52 chen_gp_x 阅读数 2430
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //统计数组相同元素的个数
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    //需要统计的数组
    NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"3",@"3",@"3", nil];
    NSSet *set = [NSSet setWithArray:array];
    for (NSString *setstring in set) {
        //需要去掉的元素数组
        NSMutableArray *filteredArray = [[NSMutableArray alloc]initWithObjects:setstring, nil];
        NSMutableArray *dataArray = array;
        NSPredicate * filterPredicate = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)",filteredArray];
        //过滤数组
        NSArray * reslutFilteredArray = [dataArray filteredArrayUsingPredicate:filterPredicate];
        //NSLog(@"Reslut Filtered Array = %@",reslutFilteredArray);
        int number = (int)(dataArray.count-reslutFilteredArray.count);
        //NSLog(@"number :%d",number);
        [dic setObject:[NSString stringWithFormat:@"%d",number] forKey:setstring];
    }
    NSLog(@"dic :%@",dic);
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //参考数组筛选:
    
    //需要去掉的元素数组
    NSMutableArray *filteredArray = [[NSMutableArray alloc]initWithObjects:@"1",@"4", nil];
    //需要被筛选的数组
    NSMutableArray *dataArray = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"1",@"4",@"6",@"1",@"1",@"4",@"1",@"6",@"4", nil];
    
    /*
     方法一:利用NSPredicate
     注:NSPredicate所属Cocoa框架,在密码、用户名等正则判断中经常用到。
     类似于SQL语句
     NOT 不是
     SELF 代表字符串本身
     IN 范围运算符
     那么NOT (SELF IN %@) 意思就是:不是这里所指定的字符串的值
     */
    NSPredicate * filterPredicate = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)",filteredArray];
    //过滤数组
    NSArray * reslutFilteredArray = [dataArray filteredArrayUsingPredicate:filterPredicate];
    NSLog(@"Reslut Filtered Array = %@",reslutFilteredArray);
    /*
     结果:
     Reslut Filtered Array = (
     2,
     6,
     6
     )
     */
    
    /*
     方法二:从后往前遍历数组,然后匹配删除
     */
    int i = (int)[dataArray count]-1;
    for(;i >= 0;i --){
        //containsObject 判断元素是否存在于数组中(根据两者的内存地址判断,相同:YES  不同:NO)
        if([filteredArray containsObject:[dataArray objectAtIndex:i]]) {
            [dataArray removeObjectAtIndex:i];
        }
    }
    NSLog(@"Data Array = %@",dataArray);
    /*
     结果:
     Data Array = (
     2,
     6,
     6
     )
     */
    
}


转载请注明出处http://blog.csdn.net/chen_gp_x

2017-06-29 10:20:33 jackieCode 阅读数 365
 

  • 第一种:利用NSDictionary的AllKeys(AllValues)方法
  • NSMutableDictionary dic = [[NSMutableDictionary alloc]initWithCapacity:0];
  • for(NSString *str in dataArray)
  • {
  • [dic setValue:str forKey:str];
  • 第二种方法:利用NSSet的AllObjects方法
  • NSSet set = [NSSet setWithArray:dataArray];
  • NSLog(@"%@",[set allObjects]);

  • 第三种方法,利用数组的containsObject来去除
  • NSMutableArray listAry = [[NSMutableArray alloc]init];
  • for (NSString *str in dataArray) {
  • if (![listAry containsObject:str]) {
  • [listAry addObject:str];
  • }
  • }
  • NSLog(@"%@",listAry);
  • 第四种方法:利用keyValue的方式
  • dataArray = [dataArray valueForKeyPath:@"@distinctUnionOfObjects.self"];
  • NSLog(@"%@",dataArray);


2014-05-16 15:55:38 betterbb 阅读数 7379

FMDB存储字典或数组时会变成字符串存入sqlite里,但如果不将其转换成json格式存储,你查询获取数据时就会获取无法转换成字典或数组的字符串。

转换JSON存储:

NSArray *picArr = (NSArray *)dic[@"pic"];

        NSError *err = nil;

        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:picArr options:NSJSONWritingPrettyPrinted error:&err];

        NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];


获取用第三方包 JSONKIT转换数组或字典

FMResultSet *set = [app.db executeQuery:@"SELECT * FROM CACHE"];

    while ([set next]) {

        NSString *str  = [set stringForColumn:@"IMGURL"];

//第三方包jsonKit转换

        NSArray *arr =(NSArray *)[str objectFromJSONString];

    }



2017-12-12 11:25:08 shihuboke 阅读数 6772

//联系人:石虎 QQ:1224614774 昵称:嗡嘛呢叭咪哄

一、概念


    NSMutableArray *tempArray = [NSMutableArray array] ;

     

    NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];

    dic1[@"code"] = @"0";

    dic1[@"value"] = @"更新时间";


    NSMutableDictionary *dic2 = [[NSMutableDictionary alloc]init];

    dic2[@"code"] = @"1";

    dic2[@"value"] = @"播放量";


    [tempArray addObject:dic1];

    [tempArray addObject:dic2];


    注意点:1.数组没有创建2.添加对象为空,3.数组越界


谢谢!!!

没有更多推荐了,返回首页