[iOS] NSArray與NSMutableArray與NSMutableDictionary

NSArray:固定長度陣列

使用固定一串資料給NSArray時,必須在陣列最後一個值放入nil,否則會發生錯誤。


範例:
NSArray *array = [ [ NSArray alloc ] initWithObjects:@”aa”,@”bbb”,nil];//宣告一陣列放入aa、bb字串
NSLog(@”array count==%d”,array.count);//印出陣列長度

for(int i = 0; i < array.count; i++){//取出陣列裡的字串並印出來
NSLog(@”i=%@”,[array objectAtIndex:i]);
}
[array release];//此陣列為暫存,且之後沒用到所以就將陣列從記憶體釋放
=================================================================
NSMutableArray:動態陣列

可以不斷放入物件的陣列,陣列長度隨著放入的物件變動
靜態宣告陣列時,一樣必須在陣列最後一個值放入nil,否則會發生錯誤。


範例1 (動態新增):

NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:@”aa”];
[array addObject:@”bbb”];

NSLog(@”array count==%d”,array.count);

for(int i = 0; i < array.count; i++){
NSLog(@”i=%@”,[array objectAtIndex:i]);
}
[array release];


範例2 (靜態放入值+動態新增)

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@”cc”,@”ddd”,nil];
[array addObject:@”aa”];
[array addObject:@”bbb”];

NSLog(@”array count==%d”,array.count);

for(int i = 0; i < array.count; i++){
NSLog(@”i=%@”,[array objectAtIndex:i]);
}
[array release];


範例3 指定index放入物件,使用insertObject指定位置時,指定的位置必須是 (陣列長度 – 1) 以內的範例值

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@”cc”,@”ddd”,nil];
[array addObject:@”aa”];
[array addObject:@”bbb”];

[array insertObject:@”aaaa” atIndex:4]; //在位置4放入 字串 aaaa


範例4:移除物件

移除指定位置物件
[array removeObjectAtIndex:4];

清除所有物件
[array removeAllObjects];


範例5:將數值放入陣列

int percentage = 40;

// 產生一個NSNumber物件,可以用signed or unsigned char, short int, int, long int, long long int, float, double or BOOL等基本型態產生物件
NSNumber *percentageObject = [NSNumber numberWithFloat:percentage];

//將NSNumber物件放入array
NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:percentageObject];

//取出數值
[percentageObject intValue];


範例6:將指定位置的物件替換掉==>replaceObjectAtIndex:索引值(int) withObject:物件(id)

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@”cc”,@”ddd”,nil];
[array addObject:@”aa”];
[array addObject:@”bbb”];

for(int i = 0; i < array.count; i++){
NSLog(@”i=%@”,[array objectAtIndex:i]);
}

[array replaceObjectAtIndex:2 withObject:@”111″];

for(int i = 0; i < array.count; i++){
NSLog(@”i=%@”,[array objectAtIndex:i]);
}
[array release];
NSDictionary及NSMutableDictionary(與java的Hashtable相似
============================================================

NSDictionary及NSMutableDictionary兩種,兩者合稱字典(dictionary),Mutable–善變的–表示可以變,NSDictionary則像是constant。NSMutableDictionary是NSDictionary的subClass。這就像一本字典,概念是(key, value),用key來搜尋出所需的value。

NSDictionary:

initWithObjectsAndKeys:

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: @”one”, [NSNumber numberWithInt: 1], @”two”,[NSNumber numberWithInt: 3], nil];

-(id) initWithObjectsAndKeys:(id) firstObject, ….

…..

第一個是firstObject的key,接著是secondObject、key,之後即為object、key的配對,直到出現nil為止。

注意:事實上nil一定出現在object的位置。若key是nil,會產生NSInvalidArgumentException。

NSMutableDictionary:

objectForKey:以下的key是由key = [objectOfNSEnumerator nextObject]; 產生的

[[[objectOfNSMutableDictionary objectForKey:key ] description] cString];

setObject: forKey:[mutable setObject:@”Tom” forKey:@”[email protected]”];

範例1: 利用NSEnumerator 取出NSMutableDictionary陣列裡所有的物件,

(1):取得所有value
NSMutableDictionary *taiStyle = [[NSMutableDictionary alloc]init];
//所有台型字串
[taiStyle setObject:[[NSNumber alloc] initWithInt:1] forKey:@”102″]; //
[taiStyle setObject:[[NSNumber alloc] initWithInt:1] forKey:@”103″]; //

NSEnumerator *enumerator = [taiStyle objectEnumerator];
id value;

while ((value = [enumerator nextObject])) {
NSLog(@”%i”,[((NSNumber*)value) intValue]);
}

(2):取得所有key
NSMutableDictionary *taiStyle = [[NSMutableDictionary alloc]init];
//所有台型字串
[taiStyle setObject:[[NSNumber alloc] initWithInt:1] forKey:@”102″]; //
[taiStyle setObject:[[NSNumber alloc] initWithInt:1] forKey:@”103″]; //

NSEnumerator *enumerator = [taiStyle keyEnumerator];
id key;

while ((key = [enumerator nextObject])) {
NSLog(@”%@”,((NSString*)key));
}


範例2:
(1)用key取出value物件,使用 valueForKey: key ,若使入的key找不到對應的key,會回傳nil

[[NSMutableDictionary *taiStyle = [[NSMutableDictionary alloc]init];

[taiStyle setObject:[[NSNumber alloc] initWithInt:1] forKey:@”102″]; //
[taiStyle setObject:[[NSNumber alloc] initWithInt:1] forKey:@”103″]; //

NSString *key = @”102″;
NSNumber *vlaue = [taiStyle valueForKey:key];
NSLog(@”%i”,[vlaue intValue] );
(2)指定key移除對應的key與value物件
[taiStyle removeObjectForKey:@”102″];//移除以@”102″字串當key的物件,key也會被移除

相關文章

寫留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *