溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

NSSet for in遍歷   OC排序

發(fā)布時間:2020-06-28 14:47:37 來源:網(wǎng)絡(luò) 閱讀:1096 作者:peipeiyi2014 欄目:開發(fā)技術(shù)

 // NSSet

   //特點:1. 存儲對象 2. 無序 3. 隨機取數(shù)據(jù)  4.元素不能重復(fù)

    //setWithObject:   類方法創(chuàng)建集合

    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(@"從字典內(nèi)取值:%@",[set anyObject]);

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

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

    }

打印結(jié)果:

 從字典內(nèi)取值:王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);

    }

    //數(shù)組里類型不一樣可以用  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的內(nèi)容

        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 語法里面不允許對數(shù)組或字典,增刪元素(開始已經(jīng)確定了要遍歷的次數(shù), 數(shù)組刪掉一個位置會發(fā)生變化,導(dǎo)致崩潰)

   for (Person * per in array) {

        [array removeObject:per];   //錯誤!!

    }

    NSLog(@"%@", array);

 */


 //***************sortedArrayUsingSelector:  數(shù)組排序方法***********************

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

    NSLog(@"%@", stringArray);

    //sortedArrayUsingSelector:

    //對字符串?dāng)?shù)組排序

    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);

    //按名字降序排列,可以在方法里調(diào)用 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問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI