码迷,mamicode.com
首页 > 其他好文 > 详细

NSset

时间:2015-07-24 12:25:29      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:

//NSset  {()}集
//不可变的
    //便利构造器创建一个集
    NSSet *set1=[NSSet setWithObjects:@"1",@"2",@"3",@"4", nil];
    NSLog(@"%@",set1);
    
    
     //使用集
    //返回元素个数
    [set1 count];
    //随机返回一个对象,但是不保证一定是随机的,一般是第一个
    NSString *s1=[set1 anyObject];
    NSLog(@"%@",s1);
    
    NSSet *set2=[NSSet setWithObjects:@"1",@"2",@"3",@"4",nil];
    
     //判断两个集是否相等,相等返回1,否则返回0
    BOOL b1=[set1 isEqualTo:set2];
    NSLog(@"%d",b1);
    //判断传入的对象是否在这个集中,在就返回这个对象,不在就返回空
    NSString *s3=[set1 member:@"10"];
    NSLog(@"%@",s3);
    
//可变的
    //创建一个空的集
    NSMutableSet *mset1=[NSMutableSet set];
    
     //添加对象
    [mset1 addObject:@"111"];
    NSLog(@"%@",mset1);
    
     //删除对象
    [mset1 removeObject:@"111"];
    NSLog(@"%@",mset1);
    
     //清空mset1里面的对象,接收新导入的对象
    [mset1 setSet:set2];
    NSLog(@"%@",mset1);
    
    */
    
    /*
//计算集里面相同元素个数(因集里面元素不可以重复,从这个可以看出集里面有多少个相同的元素)
    NSCountedSet *cSet=[[NSCountedSet alloc] initWithObjects:@"a",@"b",@"c",@"a", nil];
    NSLog(@"%@",cSet);

    
    
    
    
    //OC中的枚举就是遍历的意思
//返回输出数组中所有元素
    //NSArray *arr1=[NSArray arrayWithObjects:@"aaa",@"bbb",@"ccc",@"ddd",nil];
    NSDictionary *dict1=[ NSDictionary dictionaryWithObjectsAndKeys:@"a",@"1",@"b",@"2",@"c",@"3" ,nil];
    for (NSString *s in dict1 ){
        NSLog(@"%@",[dict1 objectForKey:s]);
    }
    
    
    
//可变数组排序
    //创建一个数组
    NSMutableArray *sortArry=[NSMutableArray arrayWithObjects:@"4",@"3",@"2",@"1", nil];
    //排序(这种方法只能从小到大排序)
    [sortArry sortUsingSelector:@selector(compare:)];
    NSLog(@"%@",sortArry);
    
    
//不可变数组排序
    //只能从小到大
    NSArray *sortArray2=[NSArray arrayWithObjects:@"4",@"3",@"2",@"1", nil];
    NSArray *arr=[sortArray2 sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"%@",arr);
    
    */
    
    
//    Student *stu1=[[Student alloc]initWithName:@"Tom" age:12];
//    Student *stu2=[[Student alloc]initWithName:@"Lucy" age:15];
//    Student *stu3=[[Student alloc]initWithName:@"Kit" age:2];
//    Student *stu4=[[Student alloc]initWithName:@"Kim" age:5];
//    
//    NSMutableArray *arr=[NSMutableArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];
//    NSLog(@"%@",arr);
//    [arr sortUsingSelector:@selector(sortUsingAge:)];
//    NSLog(@"%@",arr);
//    
    
    Student *c1=[[Student alloc]initWithName:@"小1" age:15 num:1 add:@"1区" sex:@"男" height:170 weight:60];
    Student *c2=[[Student alloc]initWithName:@"小2" age:17 num:2 add:@"2区" sex:@"女" height:185 weight:80];
    Student *c3=[[Student alloc]initWithName:@"小3" age:19 num:3 add:@"3区" sex:@"女" height:160 weight:50];
    Student *c4=[[Student alloc]initWithName:@"小4" age:21 num:4 add:@"4区" sex:@"男" height:180 weight:70];
    
    NSMutableArray *arr6=[NSMutableArray arrayWithObjects:c1,c2,c3,c4, nil];
    NSLog(@"%@",arr6);
    [arr6 sortUsingSelector:@selector(sortUsingName:)];
    NSLog(@"%@",arr6);
    [arr6 sortUsingSelector:@selector(sortUsingAge:)];
    NSLog(@"%@",arr6);
    [arr6 sortUsingSelector:@selector(sortUsingNum:)];
    NSLog(@"%@",arr6);
    [arr6 sortUsingSelector:@selector(sortUsingAdd:)];
    NSLog(@"%@",arr6);
    [arr6 sortUsingSelector:@selector(sortUsingSex:)];
    NSLog(@"%@",arr6);
    [arr6 sortUsingSelector:@selector(sortUsingHeight:)];
    NSLog(@"%@",arr6);
    [arr6 sortUsingSelector:@selector(sortUsingWeight:)];
    NSLog(@"%@",arr6);
    
    
    Man *m=[[Man alloc] initWithName:@"小王" age:15];
    Man *m1=[[Man alloc] initWithName:@"小二" age:42];
    Man *m2=[[Man alloc] initWithName:@"小三" age:25];
    Man *m3=[[Man alloc] initWithName:@"小五" age:17];
    
    NSMutableArray *m5=[NSMutableArray arrayWithObjects:m,m1,m2,m3, nil];
    [m5 sortUsingSelector:@selector(sortUsingAge:)];
    NSLog(@"%@",m5);
    
    

NSset

标签:

原文地址:http://www.cnblogs.com/-ios/p/4672983.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!