學習iOS【3】數組、詞典和集合

來源:互聯網
上載者:User

標籤:

一、數組1.不可變數組NSArray
  • arrayWithObjects:使用一組對象作為元素建立不可變數組,注意數組的最後一個值需要指定為nil,用來表示參數的結束,但是nil並不會儲存在數組中。
  • objectAtIndex:擷取指定索引位置的數組元素。
  • @[elm1,elm2….elmn]:另外一種建立數組的簡便方式,不需要以nil作為結尾元素;
  • array[index]:另外一種擷取指定索引位置元素的方式。
  • count:數組中元素個數。
 
int main(int argc, const char * argv[]){    @autoreleasepool {        /*不可變數組*/        //建立不可變數組 方式一        NSArray *arr=[NSArray arrayWithObjects:@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",@"周日",nil];        for(int i=0;i<[arr count];i++){            NSLog(@"%@",[arr objectAtIndex:i]);        }        //建立不可變數組 方式二        NSArray *[email protected][@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",@"周日"];        for(int i=0;i<[arr1 count];i++){            NSLog(@"%@",arr1[i]);        }    }}
2.可變數組NSMutableArray
  • NSMutableArray array方法建立空的可變數組,數組元素個數未定,可以隨著需要增長;
  • addObject:向可變數組結尾添加元素;
  • insertObject:obj atIndex:i :將對象obj插入數組的第i個元素。
  • removeObjectAtIndex:i  刪除數組中第i個元素。
  • replaceObjectAtIndex:i withObject:obj 將數組中序號為i的對象用obj替換。
  • 使用NSLog格式化%@可以顯示整個數組,它實際上會調用每個元素的description方法。
int main(int argc, const char * argv[]){    @autoreleasepool {        /*可變數組*/        NSMutableArray *marr=[NSMutableArray array];        for (int i=0; i<3; i++) {            marr[i][email protected](i+1);        }        [marr addObject:@(4)];        for (int i=0; i<[marr count]; i++) {            NSLog(@"%@",marr[i]);        }        NSLog(@"%@",marr);     }}

 

二、詞典對象(dictionary)詞典也有兩種類型:不可變詞典NSDictionary和可變詞典NSMutableDictionary。1.不可變詞典NSDictionary
  • NSDictionary dictionaryWithObjectsAndKeys:建立不可變詞典,參數是值-鍵對組合(注意順序),以nil結尾。
  • allKeys:返回一個數組包含詞典中的所有鍵;
  • count:返回詞典中的記錄數;
  • objectForKey:返回key的值對象。
 
int main(int argc, const char * argv[]){    @autoreleasepool {        /*不可變詞典*/        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"學生",@"1",@"教師",@"2", nil];        for (NSString *key in dic) {            NSLog(@"key:%@ value:%@",key,[dic objectForKey:key]);        }     }}
2.可變詞典NSMutableDictionary
  • NSMutableDictionary dictionary:建立一個可變詞典;
  • setObject:forKey:把索引值對添加到可變詞典中;
  • removeAllObjects:刪除詞典中所有的記錄;
  • removeObjectForKey:刪除詞典中的指定鍵key對應的記錄;
 
int main(int argc, const char * argv[]){    @autoreleasepool {        /*可變詞典*/        //建立可變詞典對象        NSMutableDictionary *mdic=[NSMutableDictionary dictionary];        //添加索引值對        [mdic setObject:@"學生" forKey:@"1"];        mdic[@"2"]=@"教師";        //顯示索引值對        NSLog(@"key:1 value is:%@",[mdic objectForKey:@"1"]);//key:1 value is:學生        NSLog(@"key:2 value is:%@",mdic[@"2"]);//key:2 value is:教師        //刪除        [mdic removeObjectForKey:@"1"];        NSLog(@"mdic count:%lu",[mdic count]);//mdic count:1        [mdic removeAllObjects];        NSLog(@"mdic count:%lu",[mdic count]);//mdic count:0     }}
三、集合對象(set)set是一組單值對象集合。1.NSSet 不可變詞典
  • setWithObjects:以一個nil為結尾的對象數組建立一個集合。
  • containObject:檢測某個對象是否包含在集合中。
  • count:集合成員個數。
 2.NSMutableSet 可變詞典
  • addObject:向集合中添加對象;
  • removeObject:從集合中刪除對象;
  • removeAllObjects:刪除集合中所有對象;
  • unionSet:求兩個集合的並集;
  • intersectSet:求兩個集合的交集
 3.執行個體首先我們自訂分類為NSSet添加自訂方法print。NSSet+Printing.h
#import <Foundation/Foundation.h>@interface NSSet (Printing)-(void)print;@end

 

NSSet+Printing.m
#import "NSSet+Printing.h"@implementation NSSet (Printing)-(void)print{    NSLog(@"------");    for(NSNumber *item in self){        NSLog(@"%li",(long)[item integerValue]);    }}@end

 

main.m
#import <Foundation/Foundation.h>#import "NSSet+Printing.h"int main(int argc, const char * argv[]){    @autoreleasepool {        //建立不可變集合        NSSet *set=[NSSet setWithObjects:@"1",@"3",@"5", @"7",nil];        //建立可變集合        NSMutableSet *mset=[NSMutableSet setWithObjects:@"1",@"2",@"3",@"4",@"5", nil];        //輸出        [set print];        [mset print];        //成員測試        if([set containsObject:@"7"]==YES){            NSLog(@"set contain 7");        }else{            NSLog(@"set does not contain 7");        }        //添加對象        [mset addObject:@"6"];        [mset print];        //移除對象        [mset removeObject:@"6"];        //求並集        [mset unionSet:set];        [mset print];        [mset print];        //求交集        [mset intersectSet:set];        [mset print];     }}
 

學習iOS【3】數組、詞典和集合

聯繫我們

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