objective-c 基礎教程 讀書筆記之第八章集合家族簡介,NSArray,NSDictionary

一:數組NSArray

     NSArray可以存儲任何objective-c的對象。當然對於int,float這些對象無法保存,另外nil對象也不能保存。

    + (id)arrayWithObjects:(id)firstObj, ...這個是產生一個數組的通常方法。

    通過索引獲取數組中的一個對象。- (id)objectAtIndex:(NSUInteger)index

    遍歷數組,會通過下面的例子來說明:

NSArray * array = [NSArray arrayWithObjects:@"one",@"two",@"three", nil];

NSString * tmp = [array objectAtIndex:2];

//遍歷數組

for (NSString * string in array) {

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

}

二:可變數組:

    跟NSString和NSMutableString的關係一樣,如果需要對數組的值進行刪除或者重置操作,需要用到可變數組 NSMutableArray。

   + (id)arrayWithCapacity:(NSUInteger)numItems  創建一個指定初始化長度的數組。

    - (void)addObject:(id)anObject 添加一個元素。

    - (void)insertObject:(id)anObject atIndex:(NSUInteger)index  在指定位置添加一個元素。

    - (void)removeObjectAtIndex:(NSUInteger)index  刪除一個數組元素。

這裏注意一點的是創建數組指定的參數並不是說數組的最大容量,系統只是根據這個值來分配一個初始的內存大小,如果有更多的元素,只管加就是了,下面的例子演示了添加更多的元素是完全ok的。

NSMutableArray * array2 = [NSMutableArray arrayWithCapacity:3];

for (int i=0; i<5; i++)

{

NSString * test = [NSString stringWithFormat:@"object %d",i];

[array2 addObject:test];

}

for (NSString * tmp in array2)

{

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

}

輸出:

array:object 0

array:object 1

array:object 2

array:object 3

array:object 4

上面的例子中,初始化指定的長度是3,我們實際添加了5個元素。

三:NSDictionary和NSMutableDictionary。字典又稱散列表或者關聯數組

  不用我說,你大概就明白了,NSMutableDictionary 於NSDictionary的區別是可變。

   dictionaryWithObjectsAndKeys: 創建一個字典。

  - (id)objectForKey:(id)aKey 獲取相應關鍵字的值。  

 NSMutableDictionary常用的幾個函數:

  + dictionaryWithCapacity:  創建一個字典

  setObject:forKey:   增加一個元素,若該key存在,則替換

  removeObjectForKey: 刪除一個元素。

四:如何往NSArray和NSDictionary中添加基本類型數據。

     我們說了,NSArray和NSDictionary只能保存對象,不能保存基本數據類型,如int,float,或者struct,要保存這些的話,就需要對其進行封裝一下:

 這裏介紹兩個封裝類:

1:NSNumber。

 + numberWithChar:

+ numberWithFloat:

+ numberWithInt:

+ numberWithBool:

對應的獲得方法:

– charValue

– floatValue

– intValue

– boolValue

例子:

NSNumber * number;

number = [NSNumber numberWithInt:34];

[array addObject:number];

2:NSValue

  通常用來包裝NSSzie,NSRect,NSPoint,和你自己創建的struct。

+ (NSValue *)valueWithBytes:(const void *)value objCType:(const char *)type  創建一個value。

傳遞的第一個參數是你想要包裝數值的地址。第二個參數是描述這個數據類型的字符串,不需要自己來想這個頭痛的參數,使用@encode指令就ok了。

例子:

NSMutableArray * array2 = [NSMutableArray arrayWithCapacity:3];

CGRect rect = CGRectMake(1,2,30,50);

CGRect rect = CGRectMake(1,2,30,50);

NSValue * value = [NSValue valueWithBytes:&rect objCType:@encode(CGRect) ];

[array2 addObject:value]; NSValue * tmpValue = [array2 lastObject];

CGRect tmpRect;

CGRect tmp2Rect;

[tmpValue getValue:&tmpRect];

tmp2Rect = [tmpValue CGRectValue ];

NSLog(@"Rect width:%@",tmpRect.size.width);

五:NSNull 

   不能在NSArray和NSDictionary中放入nil值。有些時候,確實沒值放入,怎麼辦呢,那就使用NSNull。例子:

NSMutableDictionary * profile = [NSMutableDictionary dictionaryWithCapacity:3];

[profile setObject:[NSNull null] forKey:@"phoneNumber"];

id number = [profile objectForKey:@"phoneNumber"];

if(number == [NSNull null]){

  NSLog(@"the value is nil");

}

   


  





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