Object-C—集合

來源:互聯網
上載者:User

標籤:http   io   資料   for   ar   cti   div   log   

Obejct-C中包含了三種集合,分別是:數組、字典和集(set)。

     數組和C語言中的數組相似,但是OC中的數組只能儲存物件,不能儲存基礎資料型別 (Elementary Data Type),如int、float、enum、struct等,也不能儲存nil。它也提供了編製好的索引對象,可以通過制定索引找到要查看的對象。包含可變數組(NSMutableArray)和不可變數組(NSArray)。    字典存放的是“索引值對”,即key-value,可以通過索引值尋找到字典中儲存的對象。    集儲存的也是對象,集和字典中儲存的對象都是無序的,但是字典可以通過key進行虛擬排序。    集和字典中不能存在相同的對象,如果對象一致,且是開始定義時寫入的,則後面的對象無法寫入,如果是後來添加的,則會把原來相同的對象頂替。     基本用法:
  •     數組
    初始化不可變數組:    NSArray *array = [[NSArray alloc] initWithObjects:@"one",@"Two",@"Three",nil];此數組只有三個對象,one,two,three,最後的nil可以看出結束符,並不會存入數組中。    NSLog(@"array count = %d",[array count]);  //列印數組中對象個數    [array objectAtIndex:2]    //或許索引2處的對象    初始化可變數組:NSMutableArray *Mutablearray = [NSMutableArray arrayWithCapacity:0];    //設定可變數組初始長度為0;     從一個數組拷貝到另一個數組    Mutablearray = [NSMutableArray arrayWithArray:array];   //將array的對象拷貝到Mutablearray中     在可變數組末尾添加對象    [Mutablearray addObject:@"Four"];     快速枚舉:    OC中提供了快速又集中的訪問遍曆數組、字典、集的方法,稱為快速枚舉如,現在array數組中存在的是字串的對象,所以快速枚舉如下:for(NSString *str in array){      NSLog(@"array = %@",str);    //可以一一輸出數組array中的對象}        從字串分割到數組- componentsSeparatedByString:    NSString *string = [NSString alloc] initWithString:@"One,Two,Three,Four"];

    NSLog(@"string:%@",string);    

    NSArray *array = [string componentsSeparatedByString:@","];

    NSLog(@"array:%@",array);

    [string release];

 

    //從數組合并元素到字串- componentsJoinedByString:

    NSArray *array = [NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];

    NSString *string = [array componentsJoinedByString:@","];

    NSLog(@"string:%@",string);

    刪除可變數組指定索引處的對象:

    [Mutablearray removeObjectAtIndex:1];    

     
  •     建立字典
    NSDictionary *dictionary = [NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil];

    NSString *string = [dictionary objectForKey:@"One"];

    NSLog(@"string:%@",string);

    NSLog(@"dictionary:%@",dictionary);

    [dictionary release];

 

    建立可變字典:

    //建立

    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

 

    //添加字典

    [dictionary setObject:@"One" forKey:@"1"];

    [dictionary setObject:@"Two" forKey:@"2"];

    [dictionary setObject:@"Three" forKey:@"3"];

    [dictionary setObject:@"Four" forKey:@"4"];

    NSLog(@"dictionary:%@",dictionary);

 

    //刪除指定的字典

    [dictionary removeObjectForKey:@"3"];

    NSLog(@"dictionary:%@",dictionary);

 

 

  •     集合NSSet

    //NSSet中不能存在重複的對象

    NSMutableSet *set1 = [[NSMutableSet alloc] initWithObjects:@"1",@"2",@"3", nil]; 

   NSMutableSet *set2 = [[NSMutableSet alloc] initWithObjects:@"1",@"5",@"6", nil]; 

 

    [set1 unionSet:set2];   //取並集1,2,3,5,6

    [set1 intersectSet:set2];  //取交集1

 

    [set1 minusSet:set2];    //取差集2,3,5,6

相關文章:

http://wenku.baidu.com/link?url=3wlXip1sYdUBWfXjFi58bc_MJChNoMAgA00ooevx8RQCForbdQMxHwJ43DQAAMfL_O7vgR0r1034DWLVWRGCpNjUbigqi1yM-RLEYO2R7fS

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.