温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

NSSet for in遍历   OC排序

发布时间:2020-06-28 14:47:37 来源:网络 阅读:1096 作者:peipeiyi2014 栏目:开发技术

 // NSSet

   //特点:1. 存储对象 2. 无序 3. 随机取数据  4.元素不能重复

    //setWithObject:   类方法创建集合

    Person * p1 = [[Person alloc]initWithName:@"张1" sex:@"男" age:71];

    Person * p2 = [[Person alloc]initWithName:@"李2" sex:@"男" age:22];

    Person * p3 = [[Person alloc]initWithName:@"王3" sex:@"女" age:63];

    Person * p4 = [[Person alloc]initWithName:@"钱4" sex:@"男" age:71];

    Person * p5 = [[Person alloc]initWithName:@"赵5" sex:@"男" age:22];

    NSSet * set = [NSSet setWithObjects:p1, p2, p3, p4, p5, nil];

    NSLog(@"从字典内取值:%@",[set anyObject]);

    for (int i = 0; i < 3; i++) {

        NSLog(@"%@", [set anyObject]);

    }

打印结果:

 从字典内取值:王3 女 63

 王3 女 63

 王3 女 63

 王3 女 63

   //快速枚举

    Person * p1 = [[Person alloc]initWithName:@"张1" sex:@"男" age:71];

    Person * p2 = [[Person alloc]initWithName:@"李2" sex:@"男" age:22];

    Person * p3 = [[Person alloc]initWithName:@"王3" sex:@"女" age:63];

    Person * p4 = [[Person alloc]initWithName:@"钱4" sex:@"男" age:71];

    Person * p5 = [[Person alloc]initWithName:@"赵5" sex:@"男" age:22];

    NSArray * array = [NSArray arrayWithObjects:p1, p2, p3, p4, p5, nil];

    for (Person * per in array) {

        NSLog(@"%@", per);

    }

    NSArray * stringsArray = [NSArray arrayWithObjects:@"zhangsan", @"wangwu", @"liliu",@"sunqi", nil];

    for (NSString * str in stringsArray) {

        NSLog(@"%@", str);

    }

    //数组里类型不一样可以用  id  如果只打印 Person类,需要用isKindOfClass判断一下类型

    // if (obj isKindOfClass [Person class])

    NSArray * stringsArray1 = [NSArray arrayWithObjects:@"zhangsan",p1, p2, p3, @"wangwu", @"liliu",@"sunqi", nil];

    for (id obj in stringsArray1) {

        NSLog(@"%@", obj);

    }


 张1 男 71

 李2 男 22

 王3 女 63

 钱4 男 71

 赵5 男 22

 zhangsan

 wangwu

 liliu

 sunqi

 zhangsan

 张1 男 71

 李2 男 22

 王3 女 63

 wangwu

 liliu

 sunqi


  //遍历字典  for in

    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"name", @"男", @"sex", @"20", @"age", nil];

    for (NSDictionary * key in dic) {  //遍历字典,只能遍历每个key的内容

        NSString * value = [dic objectForKey:key];

        NSLog(@"%@", value);

    }

zhangsan

20


  Person * p1 = [[Person alloc]initWithName:@"张1" sex:@"男" age:71];

    Person * p2 = [[Person alloc]initWithName:@"李2" sex:@"男" age:22];

    Person * p3 = [[Person alloc]initWithName:@"王3" sex:@"女" age:63];

    Person * p4 = [[Person alloc]initWithName:@"钱4" sex:@"男" age:71];

    Person * p5 = [[Person alloc]initWithName:@"赵5" sex:@"男" age:22];

   NSMutableArray * array = [NSMutableArray arrayWithObjects:p1, p2, p3, p4, p5, nil];

  /*******************************************************************************************************************************************************************************************

   运行崩溃, for in 语法里面不允许对数组或字典,增删元素(开始已经确定了要遍历的次数, 数组删掉一个位置会发生变化,导致崩溃)

   for (Person * per in array) {

        [array removeObject:per];   //错误!!

    }

    NSLog(@"%@", array);

 */


 //***************sortedArrayUsingSelector:  数组排序方法***********************

    NSArray * stringArray = [NSArray arrayWithObjects:@"lisi", @"wangwu", @"cuiyayun", @"xiaohao", @"yihunyun", @"liuyan", @"gobin", @"jifeifei", @"fangdatong", @"fanbingbing", nil];

    NSLog(@"%@", stringArray);

    //sortedArrayUsingSelector:

    //对字符串数组排序

    NSArray * newArray = [stringArray sortedArrayUsingSelector:@selector(compare:)];

    NSLog(@"%@", newArray);

2015-01-17 23:25:58.736 OC 一月16号之2[2747:303] (

    lisi,

    wangwu,

    cuiyayun,

    xiaohao,

    yihunyun,

    liuyan,

    gobin,

    jifeifei,

    fangdatong,

    fanbingbing

)

2015-01-17 23:25:58.738 OC 一月16号之2[2747:303] (

    cuiyayun,

    fanbingbing,

    fangdatong,

    gobin,

    jifeifei,

    lisi,

    liuyan,

    wangwu,

    xiaohao,

    yihunyun

)


Person * p1 = [[Person alloc]initWithName:@"zhangsan" sex:@"男" age:71];

    Person * p2 = [[Person alloc]initWithName:@"lier" sex:@"男" age:22];

    Person * p3 = [[Person alloc]initWithName:@"wangsan" sex:@"女" age:63];

    Person * p4 = [[Person alloc]initWithName:@"qiansi" sex:@"男" age:76];

    Person * p5 = [[Person alloc]initWithName:@"zhaowu" sex:@"男" age:59];

    

    NSArray * array = [NSArray arrayWithObjects:p1, p2, p3, p4, p5, nil];

    //按年龄进行排序

    NSArray * newArray = [array sortedArrayUsingSelector:@selector(sortByAge:)];

    NSLog(@"%@", newArray);

    //按名字降序排列,可以在方法里调用 compare:

    NSArray * newArray1 = [array sortedArrayUsingSelector:@selector(sortByName:)];

    NSLog(@"%@", newArray1);

附:

- (NSComparisonResult)sortByAge:(Person *)p

{

    if (_age < [p age]) {

        return NSOrderedAscending;//-1升序

    }

    else if(_age > [p age])

        return NSOrderedDescending; //1 降序

    else return NSOrderedSame;

}


- (NSComparisonResult)sortByName:(Person *)p

{

    return [[p name] compare:_name];

}

年龄降序: (

    "lier \U7537 22",

    "zhaowu \U7537 59",

    "wangsan \U5973 63",

    "zhangsan \U7537 71",

    "qiansi \U7537 76"

)

名字降序 (

    "zhaowu \U7537 59",

    "zhangsan \U7537 71",

    "wangsan \U5973 63",

    "qiansi \U7537 76",

    "lier \U7537 22"

)


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI