zoukankan      html  css  js  c++  java
  • 集合

    1.OC集合类包括NSArray,NSSet,NSDictionary都是以面向对象的方式操作数组,而且OC数组不像C语言中的数组只能存放同一种数据类型,它可以存放任意类型的对象,但是不能存放非OC对象类型如基本数据类型int,struct,enum等NSArray 用于对象有序集合(相当于是数组)NSSet 用于对象无序集合NSDictionary用于键值映射

    ////       集合

    ////        初始化

    //        NSArray *name=[NSArray arrayWithObjects:@"lisi",@"aulu",@"alji",@"dakk", nil];

    //        

    ////        集合元素个数

    //        NSLog(@"%ld",[name count]); //集合元素个数。

    //       NSString *name1 =[name objectAtIndex:1];//获取指定位置的集合元素。

    //        NSLog(@"%@",name1);

    ////        循环遍历集合

    ////        i<[name count]=i<name.count

    ////        for (int i=0; i<[name count]; i++) {

    ////            NSLog(@"%@",name[i]);

    ////        }

    ////        快速枚举 循环遍历集合

    //        for (NSString *str in name) {

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

    //        }

    //        

    ////        可变集合添加元素。

    //        NSMutableArray *name2=[NSMutableArray arrayWithCapacity:0];

    //        [name2 addObject:@"zhangsds"];

    //        [name2 addObject:@"lisi"];

    ////        插入指定位置元素

    //        [name2 insertObject:@"dasd" atIndex:1];

    //        for (NSString *name3 in name2) {

    //            NSLog(@"%@",name3);

    //        }

    //        

    //        

    //        NSMutableArray *test1=[NSMutableArray array];

    //        NSArray *test2=@[@"das",@"adsa",@"casf"];

    //        [test1 addObjectsFromArray:test2];

    //        [test1 addObject:@"dasf"];

    ////        删除集合元素

    //        [test1 removeObject:@"das"];

    //        [test1 removeObjectAtIndex:2];

    ////        替换

    //        [test1 replaceObjectAtIndex:1 withObject:@"fafafkj"];

    //        

    //       NSString *str1=[test1 componentsJoinedByString:@"+"];

    //        NSLog(@"%@",str1);

    //        

    ////        将字符串拆分成集合

    //        NSArray *test3=[str1 componentsSeparatedByString:@"-"];

    //        NSLog(@"%@  ",test3);

            NSArray *names=[NSArray arrayWithObjects:@"zhangsan",@"lisi",@"wangwu", nil];

            NSSortDescriptor *arr=[NSSortDescriptor sortDescriptorWithKey:nil ascending:YES];

            NSArray *sortarr=[names sortedArrayUsingDescriptors:@[arr]];

            for (NSString *obj in sortarr) {

                NSLog(@"%@",obj);

            }

            

    //        选择器

            NSArray *newarr=[names sortedArrayUsingSelector:@selector(compare:)];

            NSLog(@"%@",newarr);

            

    //        代码块排序法

            NSArray *ar=[names sortedArrayUsingComparator:^NSComparisonResult

                         (id obj1,id obj2){return [obj1 compare:obj2];}];

            NSLog(@"%@",ar);

    ////        相交

            NSMutableSet *set2=[NSMutableSet setWithObjects:@"zhan",@"lili",@"das",nil];

            NSMutableSet *set3=[NSMutableSet setWithObjects:@"zhan",@"das",nil];

            [set2 intersectSet:set3];

          

            NSLog(@"%@",set2);

     

    //        字典

            NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"lisi",@"name",@"gy",@"addr", nil];

            

            NSLog(@"%@",dic);

    //        通过关键字获取value值

            NSLog(@"%@",[dic objectForKey:@"name"]);

            

            

    //        可变字典

            

            NSMutableDictionary *dic1=[NSMutableDictionary dictionaryWithCapacity:100];

    //        添加值

            [dic1 setObject:@"guiyang" forKey:@"addr"];

            [dic1 setObject:@"lii" forKey:@"name"];

            NSLog(@"%@",dic1);

            [dic1 removeObjectForKey:@"addr"];

            NSLog(@"%@",dic1);

            

            NSMutableDictionary *dic2=[NSMutableDictionary dictionaryWithCapacity:100];

            

            [dic1 setObject:@"guiya" forKey:@"addr"];

            [dic1 setObject:@"li" forKey:@"name"];

            NSLog(@"%@",dic2);

            NSArray *arr=@[dic1,dic2];

     

  • 相关阅读:
    java继承
    Linux下word转pdf以及unoconv中文乱码问题
    jquery点击事件捕获
    在Windows上玩TensorFlow(一)——安装Docker【转】
    php面试总结
    [转载] PHP 线程,进程和并发
    微信小程序数据解密
    sql基础整理
    PHP 底层的运行机制与原理【转载】
    react-native 运行原理【转载】
  • 原文地址:https://www.cnblogs.com/tianlianghong/p/5240177.html
Copyright © 2011-2022 走看看