NSArray NSDictionary NSMutableArray NSMutableDictionary

一、NSArray

NSArray 是不可變的,而且只能存儲Object-c對象。另外,數組的最後一個元素一定是nil,表示結束。

注:這些集合類只能收集cocoa對象(NSObject對象),如果想保存一些原始的C數據(例如,int,float,double,BOOL等),則

需要將這些原始的C數據封裝成NSNumber類型的,NSNUmber對象是cocoa對象,可以被保存在集合類中。

數組常見的用法首先是創建數組,然後對數組進行操作。

對數組進行的常見操作可以分爲:增(插入)、刪、改、查、取、遍歷、排序、比較。

對於NSArray來說,因爲本身是不可變的,所以對NSArray的操作沒有增和刪。

1.NSArray的聲明與初始化

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

NSArray * array1 =[NSArray arrayWithObjects:@"One",@"Two",nil];//不需要手動釋放

 //- (unsigned) Count;數組所包含對象個數;

 NSLog(@"%ld",[arraycount]);

 //- (id) objectAtIndex: (unsigned int) index;獲取指定索引處的對象;

 NSLog(@"%@",[arrayobjectAtIndex:2]);

 [array release];

 //----從一個數組拷貝數據到另一個數組(可變數組

 NSMutableArray * MutableArray1 = [[NSMutableArray alloc]init];

 NSArray * array2 =[NSArray  arrayWithObjects:@"a",@"b",@"c",nil];

 MutableArray1 = [NSMutableArray  arrayWithArray:array2];

 NSLog(@"%@",MutableArray1);

//NSArray初始化的時候也可以使用arrayWithArray

NSArray * array3 = [[NSArrayalloc] init];

array3 =[NSArray arrayWithArray:array1];

NSLog(@"%@",array3);

    

2.NSArray的遍歷  通常有三種方式  1.in    2.迭代器   3.for  

id obj;

NSMutableArray * newArray =[ [NSMutableArray alloc]init];

NSArray * oldArray =[NSArrayarrayWithObjects:@"a",@"b",@"d",@"c",nil];

for (int i=0; i<[oldArray count]; i++) {

       obj=[[oldArray objectAtIndex:i]copy];

        [newArray addObject:obj];

}

//快速枚舉

for (id obj1in oldArray) {

            [newArray addObject:obj1];

 }

3.排序

/*

1.字符串數組排序

IOS提供一個sortedArrayUsingSelector ()函數,用於對字符串數組進行排序,方法會在排序後返回一個新的數組。

[array  sortedArrayUsingSelector (@selector(xxxx:))];

xxxx:方法需要你自己根據實際情況設定排序方式。有默認的排序方法 compare :

2.數字數組排序

將數字加入數組,首先要轉換成NSNumber。然後再使用sortedArrayUsingSelector:

注意取出數字元素的時候要換一下類型

NSNumber *indicator = (NSNumber *)[ numberArray  objectAtIndex:idx];

int  value  = [ indicator intValue ];

3.對對象數組排序

可以使用NSSortDescriptor對對象數組進行排序。

@interface  Person :NSObject {

 NSString *firstName;

 NSString *lastName;

}

 @property (nonatomic , retain ) NSString *firstName ;

 @property (nonatomic , retain ) NSString *lastName ;

 現在,有一個由Person對象組成的數組,對這個數組排序,使用sortUsingDescriptors:             

NSSortDescriptor *sorter = [ NSSortDescriptor  alloc ] initWithKey : xxx   ascending:YES];                                            

[array   sortUsingDescriptors : [ NSArray  arrayWithObject:sorter ]] ;

*/

 [newArray sortUsingSelector:@selector(compare:)];

 NSLog(@"%@",newArray);

4.切分數組

//從字符串分割到數組componentsSeparateByString

NSString * string = [[NSStringalloc] initWithString:@"One,Two,Three,Four"];

NSArray * array4 = [stringcomponentsSeparatedByString:@","];

NSLog(@"%@",array4);

[string release];


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

NSArray * array5 =[NSArrayarrayWithObjects:@"One",@"Two",@"Three",@"Four",nil];

NSString * string2 =[array5componentsJoinedByString:@","];

NSLog(@"%@",string2);



二、NSMutableArray(可變數組)

2.1 初始化NSMutableArray

//給數組分配容量

NSMutableArray * array6;

array6 = [NSMutableArrayarrayWithCapacity:20];

//在數組末尾添加對象

array6 =[NSMutableArrayarrayWithObjects:@"One",@"Two",@"Three",nil];

[array6 addObject:@"Four"];

NSLog(@"%@",array6);

2.2刪除數組中指定索引處對象

[array6 removeObjectAtIndex:1];

NSLog(@"%@",array6);

2.3遍歷數組

//- (NSEnumerator *)objectEnumerator;從前向後

NSMutableArray * array7 =[NSMutableArrayarrayWithObjects:@"One",@"Two",@"Three",nil];

NSEnumerator * enumerator;

enumerator = [array7 objectEnumerator];

id temp;

while(temp=[enumeratornextObject]){

       NSLog(@"%@",temp);

}

//- (NSEnumerator *)reverseObjectEnumerator;從後向前

NSEnumerator * enumerator1;

enumerator1 =[array7 reverseObjectEnumerator];

while(temp=[enumerator1nextObject]){

      NSLog(@"%@",temp);

}

//快速枚舉

for(NSString * string3in array7){

     NSLog(@"%@",string3);

}



三、NSDictionary(不可變詞典)

 [NSDictionary dictionaryWithObjectsAndKeys:..] :使用鍵值對兒直接創建詞典對象,結尾必需使用nil標誌結束。

 [NSDictionary initWithObjectsAndKeys:..] :使用鍵值對兒初始化詞典對象,結尾必需使用nil標誌結束。

 [dictionary count]: 得到詞典的長度單位。

 [dictionary keyEnumerator]: 將詞典的所有KEY儲存在NSEnumerator中,NSEnumerator很像Java語言中的迭代器,使用快速枚舉可以遍歷詞典中所有儲存KEY值。

[dictionary  objectEnumerator]: 將詞典的所有value儲存在NSEnumerator,用法和上面差不多可用來遍歷KEY對應儲存的Value值。

         [dictionary objectForKey:key]:通過傳入KEY對象可以拿到當前KEY對應儲存的值。

3.1NSDictionary的聲明與初始化

//- (id) initWithObjectsAndKeys;

NSDictionary * dictionary =[[NSDictionaryalloc]initWithObjectsAndKeys:@"geek",@"name",@"110",@"Tel",@"18",@"age",nil];

//得到字典的數量

NSUInteger count =[dictionarycount];

NSLog(@"字典的數量爲:%ld",count);

//得到詞典中所有KEY

NSEnumerator * enumeratorKey = [dictionarykeyEnumerator];

for(NSObject * objectin enumeratorKey){

        NSLog(@"遍歷KEY的值%@",object);

 }

//得到詞典中所有Value

NSEnumerator * enumeratorValue = [dictionaryobjectEnumerator];

for(NSObject * objectin enumeratorValue){

      NSLog(@"遍歷Value的值%@",object);

 }

//通過KEY找到value  

NSString * string3 =[dictionaryobjectForKey:@"name"];

if(string3 !=nil){

    NSLog(@"%@",string3);

}

   NSLog(@"%@",dictionary);


四、NSMutableDictionary(可變詞典)

NSMutableDictionary NSDictionary的子類,所以繼承了NSDictionary的方法。

4.1 NSMutableDictionary 的聲明與初始化

NSMutableDictionary * MutableDictionary = [NSMutableDictionarydictionaryWithCapacity:10];// 創建一個可變詞典初始指定它的長度爲10.,動態的添加數據如果超過10這個詞典長度會自動增加,所以不用擔心數組越界。

//向詞典中動態添加數據

[MutableDictionary setObject:@"Lucy"forKey:@"name"];

[MutableDictionary setObject:@"110"forKey:@"Tel"];

//通過KEY找到value

NSString * string4 =[MutableDictionaryobjectForKey:@"name"];

if(string4 !=nil){

    NSLog(@"%@",string4);

}

//[dictionary removeObjectForKey..] :刪除掉詞典中指定KEY的數據。

NSLog(@"%@",MutableDictionary);

[MutableDictionary removeObjectForKey:@"name"];

 NSLog(@"%@",MutableDictionary);

//[dictionary removeAllObjects..] :刪除掉詞典中的所有數據。

[MutableDictionary removeAllObjects];

NSLog(@"%@",MutableDictionary);

 //詞典類的存在就是爲了解決在大量數據中查找方便,因爲它是通過key直接找到value所以速度很快,避免一個個的遍歷尋找造成的效率低下,靈活的使用詞典類會幫你的程序提速。





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章