IOS階段學習第16天筆記(Category/NSSet/NSIndexSet 操作),nssetnsindexset

來源:互聯網
上載者:User

IOS階段學習第16天筆記(Category/NSSet/NSIndexSet 操作),nssetnsindexset

IOS學習(OC語言)知識點整理

 


一、NSSet、NSMutableSet集合的介紹

 

1)NSSet、NSMutableSet集合,元素是無序的,不能有重複的值。

 

2)用執行個體方法建立一個不可變集合對象 例如:

//宏定義#define TOBJ(n) [NSNumber numberWithInt:n]NSSet *set1=[[NSSet alloc]initWithObjects:TOBJ(2),TOBJ(3),TOBJ(3),TOBJ(1),TOBJ(5), nil];

 

2)用類方法建立一個不可變集合對象 例如:

 

1 NSSet *set2=[NSSet setWithObjects:TOBJ(2),TOBJ(4),TOBJ(6), nil];

  

3)NSSet 快速遍曆方法(無序,所以沒有下標)例如:

 

1  for(id num in set1){2     NSLog(@"%@",num);3 } 

  

4)setSet 用於修改集合內容 例如:[mSet setSet:set1];


5)intersectSet 用於擷取兩個集合的交集(返回兩個集合中相同的元素)。例如:

 

#define TOBJ(n) [NSNumber numberWithInt:n]NSSet *set1=[[NSSet alloc]initWithObjects:TOBJ(2),TOBJ(3),TOBJ(3),TOBJ(1),TOBJ(5), nil];NSSet *set2=[NSSet setWithObjects:TOBJ(2),TOBJ(4),TOBJ(6), nil];[mSet intersectSet:set2];NSLog(@"intersect:%@",mSet); //結果:2

  

6)unionSet 用於擷取兩個集合的並集(返回兩個集合中所有的元素,如果重複只顯示其中一個) 例如:

 

1 #define TOBJ(n) [NSNumber numberWithInt:n]2 NSSet *set1=[[NSSet alloc]initWithObjects:TOBJ(2),TOBJ(3),TOBJ(3),TOBJ(1),TOBJ(5), nil];3 NSSet *set2=[NSSet setWithObjects:TOBJ(2),TOBJ(4),TOBJ(6), nil];4 [mSet intersectSet:set2];5 NSLog(@"intersect:%@",mSet); //結果:123456

  

7)minusSet 用於擷取兩個集合的差集 例如:

 

1 #define TOBJ(n) [NSNumber numberWithInt:n]2 NSSet * mSet =[[NSSet alloc]initWithObjects:TOBJ(2),TOBJ(3),TOBJ(3),TOBJ(1),TOBJ(5), nil];3 NSSet *set2=[NSSet setWithObjects:TOBJ(2),TOBJ(4),TOBJ(6), nil];4 [mSet minusSet:set2];5 NSLog(@"intersect:%@",mSet); //結果:13456

  

8)allObjects 用於將集合轉換為數組 例如:

 

1 #define TOBJ(n) [NSNumber numberWithInt:n]2 NSSet * mSet =[[NSSet alloc]initWithObjects:TOBJ(2),TOBJ(3),TOBJ(3),TOBJ(1),TOBJ(5), nil];3 NSArray *array= [mSet allObjects];

  

9)anyObject 取set中任意一個元素(如果set中只有一個元素,取值)

 

1 #define TOBJ(n) [NSNumber numberWithInt:n]2 NSSet * mSet =[[NSSet alloc]initWithObjects:TOBJ(2),TOBJ(3),TOBJ(3),TOBJ(1),TOBJ(5), nil];3 id value=[mSet anyObject];

  

二、NSIndexSet、NSMutableIndexSet 可變索引集合的介紹


1)索引集合,表示唯一的整數的集合,有可變和不可變之分。


2)initWithIndexesInRange 用指定的範圍對應的索引建立索引對象 例如:

 

1 NSIndexSet *indexSet1=[[NSIndexSet alloc]initWithIndexesInRange:2 NSMakeRange(2,   3)];//結果 2,3,4

  

3)objectsAtIndexes 根據索引集合中的索引取出數組中對應的元素(返回數組) 例如:

 

1 NSIndexSet *indexSet1=[[NSIndexSet alloc]initWithIndexesInRange:NSMakeRange(2,   3)]; 2 NSArray *array=@[@"one",@"two",@"three",@"four",@"five",@"sex"];3 NSArray *array2=[array objectsAtIndexes:indexSet1];4 NSLog(@"array2:%@",array2); //結果:array2:three four five

  

4)建立一個可變的集合索引(初始化時有一個索引)(可以儲存不連續的索引值) 例如:

 

1 NSMutableIndexSet *indexSet2=[NSMutableIndexSet indexSetWithIndex:2];2 [indexSet2 addIndex:4];3 [indexSet2 addIndex:1];4 [indexSet2 addIndex:2];5 NSLog(@"count:%ld",indexSet2.count);//擷取個數6 NSArray *array3=[array objectsAtIndexes:indexSet2];//結果:two three  five

  

5)NSNull:類表示空, 只有一個類方法[NSNull null]擷取Null 物件,在數組中nil表示元素結束(不能用nil表示空元素

      可採用[NSNull null]表示空元素) 例如:

 

1 NSArray *array5=[NSArray arrayWithObjects:@"red",[NSNull null],@"yellow",@"blue", nil];

  

三、Category 介紹


1)Category 意為: 類別、分類、類目

 1、可以在不改變類名的情況下,擴充類的功能(給類增加方法)

 2、可以將類的功能拆成多個檔案編譯

 3、類別中不能增加成員變數,可以訪問原來類中的成員變數

 4、類別中可以增加與原來類中同名的方法,調用時優先調用

 5、添加檔案時選擇Objective-C File 那個檔案同時注意選擇要拓展的類名


2)類別的聲明類似於類的聲明,@interface要擴充功能的類名(類別名)

     1、類別不能執行個體化對象 

     2、類別中不能增加成員變數。

     3、類別中的方法可以訪問原來類中的成員變數

     4、類別可以調用原來類中的方法

     5、類別中的方法可以被子類繼承

     6、類別可以添加與原來類中相同的方法,調用時類別中的方法優先調用,一般不建議這樣操作(無法再調用原來類中的方法)


3)字串、NSNumber是簇類,底層是由很多類組成的,不能有子類 ,因為子類調用不了父類中的方法


4)Category 檔案名稱格式為:父類檔案名稱+子類檔案名稱 如:NSMutaleString+Resvrse.h


5)Category .m檔案中的方法表現形式: 1 @implementation NSMutableString (Reverse) 


四、Extension 的介紹


1)extension:相當於未命名的Category,可以擴充類的功能(增加方法),也可以增加成員變數。


2)extension:只有.h檔案


3)extension  表現形式 @interface 類名()例如: 1 @interface Person (){} 


4)在.m檔案中也可以聲明成員變數,不會將其放在介面h檔案中暴露給使用者。 例如:

1 @interface Person()2 { 3     int _num;4 }5 //將方法聲明為私人的6 -(void)print2;7 @end

 

五、SEL的介紹


1)SEL是一種類型,將方法名封裝為sel的變數,通過SEL找到方法的地址,調用方法。


2)SEL 封裝方法執行個體代碼:

 

1  //將play方法名封裝成SEL類型的資料2 SEL sel=@selector(play);3  //判斷p1所屬的類是否實現了sel中的方法4 if([p1 respondsToSelector:sel]){5  //p1尋找sel中方法的地址,再調用對應的方法6 [p1 performSelector:sel];7  }

  

3)performSelector 用於執行SEL封裝的方法 例如: 1 [p1 performSelector:@selector(jump)]; 


4)SEL 封裝帶參數的方法執行個體代碼:

 

1  //將帶一個參數的方法封裝為SEL的變數,執行,參數是id類型2  [p1 performSelector:@selector(print:) withObject:@"hello"];

  

5)NSSelectorFromString 用於將字串形式的方法名封裝成SEL的資料 執行個體代碼:

  

1  SEL sel2=NSSelectorFromString(@"study");2  [p1 performSelector:sel2];

  

6)_cmd 表示當前執行的方法 例如: 1 NSLog(@"*****metheod:%@",NSStringFromSelector(_cmd));  


7)在C語言中 __func 表示擷取當前執行方法 例如: 1 NSLog(@"func=%s",__func__);  

  __DATE__ 表示擷取當前系統時間  1 NSLog(@"date=%s",__DATE__); 


8)SEL實現數組排序 執行個體代碼: 

1 void testSel()2 {3     Person *p1;4     Person *p2;5     Person *p3;6     NSMutableArray *array1=[[NSMutableArray alloc]initWithObjects:p1,p2,p3, nil];7     [array1 sortUsingSelector:@selector(comparePerson:)];8     9 }

  

六、構造OC中的二維數組 執行個體代碼:

 

//建立一個空的外層數組 2         NSMutableArray *bigArray=[NSMutableArray array]; 3         //建立一個存放4個資料對象的數組 4         NSMutableArray *array1=[[NSMutableArray alloc]init]; 5         for(int i=0;i<4;i++){ 6             [array1 addObject:[NSNumber numberWithInt:i]]; 7         } 8         //建立一個存放3個字串的數組 9         NSMutableArray *array2=[[NSMutableArray alloc]init];10         for(int i=0;i<3;i++){11             [array2 addObject:[NSString stringWithFormat:@"str%d",i+1]];12         }13         //將array1和array2兩個數組對象存入外層數組(相當於建立了一個二維數組)14         [bigArray addObject:array1];15         [bigArray addObject:array2];16         17         //遍曆,顯示所有的元素18         for(int i=0;i<bigArray.count;i++){19             for(int j=0;j<[bigArray[i] count];j++){20                 //取出數組中第i行第j列的元素(每行又是一個數組對象)21                 if([bigArray[i][j] isKindOfClass:[NSNumber class]]){22                     NSLog(@"number:%@",bigArray[i][j]);23                 }24                 else if ([[[bigArray objectAtIndex:i] objectAtIndex:j] isKindOfClass:[NSString class]]){25                     NSLog(@"string:%@",[[bigArray objectAtIndex:i] objectAtIndex:j]);26                 }27             }28         }

  

七、Class (類)的介紹

 

 1)類的本質也是一個對象,是Class類型的對象,擷取類對象(可以通過執行個體方法或類方法擷取),

       每個類只有一個類對象。


  2)load 方法當程式啟動時會載入所有的類和分類,調用load方法,先載入父類,再載入子類,然後是分類 例如:

 

 1  +(void)load2  {3      NSLog(@"Person---load");4  }

   

 3)initialize方法 當第一次使用類的時候,調用initialize方法,先調用父類的,再調用子類的 例如:

 

1 +(void)initialize2  {3     NSLog(@"Person---initialize");4  }

  

相關文章

聯繫我們

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