NSArray
一個NSArray對象用來包含指向其他對象的指針列表。它裏面的元素都有一個唯一的index。
如果有n個對象,index就是從0到(n-1)。在NSArray中,是不沒有nil。
NSArray從NSObject繼承下來。無法刪除,添加,也無法修改。
NSArray常用方法:
-(unsigned)count//得到array中對象個數
-(id)objectAtIndex:i //獲得索引爲i的對象。注意i不要越界了。
-(id)lastObject//得到數組中最後一個對象。如果數組沒有對象,返回nil
-(BOOL)containsObject:(id)anObject//判斷anObject是否在數組內。是返回YES。
-(unsigned)indexOfObject:(id)anObject//查找數組中有沒有anObject。有返回最小索引。
NSMutableArray
NSMutableArray繼承NSArray。NSMutableArray可以增加,刪除對象。
簡單的可以用NSArray中的mutableCopy複製得到一個可以修改的NSMutableArray。
NSMutableArray常用方法:
- (void)addObject:(id)anObject
- (void)addObjectsFromArray:(NSArray *)otherArray//將otherArray加入到數組後面
- (void)insertObject:(id)anObject atIndex:(unsigned)index//在索引index處插入anObject,index後面的所有對象向後移動, anObject 不能爲nil
- (void)removeAllObjects
- (void)removeObject:(id)anObject
- (void)removeObjectAtIndex:(unsigned)index
如果真的要給數組加入一個空的對象。可以用NSNull
[myArray addObject:[NSNull null]];