[oc學習筆記]數組的遍曆和排序,oc學習筆記

來源:互聯網
上載者:User

[oc學習筆記]數組的遍曆和排序,oc學習筆記

 1         //數組的遍曆(可變數組舉例) 2         //for遍曆 3         NSMutableArray *aa = [NSMutableArray array]; 4         [aa addObjectsFromArray:@[@"ss",@"ddd",@"sad"]]; 5         for (int i = 0;  i < aa.count; i++) { 6             NSLog(@"%@",[aa objectAtIndex:i]); 7         } 8         //快速遍曆 9         for (id obj in aa) {10             NSLog(@"%@",obj);11         }12         //代碼塊遍曆13         // obj是對象 idx是下標 stop是停止標誌14         [aa enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {15             NSLog(@"%@",obj);16             NSLog(@"%@",[aa objectAtIndex:idx]);17         }];18         //迭代器遍曆19         //將數組元素存放進迭代器20         NSEnumerator *en = [aa objectEnumerator];21         id obj = nil;//建立一個對象接受返回對象22         while (obj = [en nextObject]) {23             NSLog(@"%@",obj);24         }25         //反向迭代器遍曆26         NSEnumerator *en1 = [aa reverseObjectEnumerator];27         id obj1 = nil;28         while (obj1 = [en1 nextObject]) {29             NSLog(@"%@",obj1);30         }31         32         33         //排序(可變數組舉例)34         //可變數組排序的方法無傳回值 不可變數組的排序有傳回值35         //選取器排序36         //compare方法盡限於比較元素是字元時 ,如有另外的比較 請另寫比較方法 傳回值(NSComparisonResult)37         [aa sortUsingSelector:@selector(compare:)];38         //代碼塊排序39         //根據實際情況寫判斷方法40         [aa sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {41             return [obj1 compare:obj2];42         }];43         //比較描述器排序44         //對多個條件進行排序比較便利45         Person *p1 = [[Person alloc]init];46         p1.name = @"ss";47         p1.age = @"16";48         49         Person *p2 = [[Person alloc]init];50         p2.name = @"dd";51         p2.age = @"14";52         53         Person *p3 = [[Person alloc]init];54         p3.name = @"hh";55         p3.age = @"22";56         57         Person *p4 = [[Person alloc]init];58         p4.name = @"hh";59         p4.age = @"19";60         NSMutableArray *sss = [NSMutableArray arrayWithObjects:p1,p2,p3,p4,nil];61         NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];62         NSSortDescriptor *sort2 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];63         NSArray *s = @[sort1,sort2];64         [sss sortUsingDescriptors:s];65         NSLog(@"%@",sss);

person.h

1 #import <Foundation/Foundation.h>2 3 @interface Person : NSObject4 @property (nonatomic,strong)NSString *name;5 @property (nonatomic,strong)NSString *age;6 @end

person.m

1 #import "Person.h"2 3 @implementation Person4 - (NSString *)description5 {6     return [NSString stringWithFormat:@"%@ %@", _name,_age];7 }8 @end

運行結果分析:

2015-06-01 17:37:55.984 Array[2030:140148] ss2015-06-01 17:37:55.985 Array[2030:140148] ddd2015-06-01 17:37:55.985 Array[2030:140148] sad2015-06-01 17:37:55.986 Array[2030:140148] ss2015-06-01 17:37:55.986 Array[2030:140148] ddd2015-06-01 17:37:55.986 Array[2030:140148] sad2015-06-01 17:37:55.986 Array[2030:140148] ss2015-06-01 17:37:55.986 Array[2030:140148] ss2015-06-01 17:37:55.986 Array[2030:140148] ddd2015-06-01 17:37:55.987 Array[2030:140148] ddd2015-06-01 17:37:55.987 Array[2030:140148] sad2015-06-01 17:37:55.987 Array[2030:140148] sad2015-06-01 17:37:55.992 Array[2030:140148] ss2015-06-01 17:37:55.992 Array[2030:140148] ddd2015-06-01 17:37:55.992 Array[2030:140148] sad2015-06-01 17:37:55.992 Array[2030:140148] sad2015-06-01 17:37:55.992 Array[2030:140148] ddd2015-06-01 17:37:55.993 Array[2030:140148] ss2015-06-01 17:37:55.993 Array[2030:140148] (    "dd 14",    "hh 19",    "hh 22",    "ss 16")

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.