Objective-C 学习笔记 - 数组与可变数组

2018-08-27  本文已影响0人  遠遊旳遊子

【前言】

关键词:NSMutableArray : NSArray

// 常见创建方法
- (id)initWithObjects:(id)firstObj, ... ;
+ (id)arrayWithObjects:(id)firstObj, ... ;
// 获取数组元素个数
- (NSUInteger)count;
// 通过索引获取相应的元素
- (id)objectAtIndex:(NSUInteger)index;
// 通过对象地址获取在数组中的索引
- (NSUInteger)indexOfObject:(id)anObject;
// 判断数组中数组包含元素anObject
- (BOOL)containsObject:(id)anObject;
// 获取数组的最后一个元素
- (id)lastObject;
// 把数组元素内容按照字符串separator进行拼接
- (NSString *)componentsJoinedByString:(NSString *)separator;
<1>增加数组元素
// 追加元素
- (void)addObject:(id)anObject;
// 指定索引插入元素
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
// 追加一个数组
- (void)addObjectsFromArray:(NSArray *)otherArray;
<2>删除
// 删除最后一个元素
- (void)removeLastObject;
// 删除指定索引的元素
- (void)removeObjectAtIndex:(NSUInteger)index;
// 删除所有元素
- (void)removeAllObjects;
//在一定范围删除指定的元素
- (void)removeObject:(id)anObject inRange:(NSRange)range;
// 删除指定的元素
- (void)removeObject:(id)anObject;
// 根据一个数组删除指定的元素
- (void)removeObjectsInArray:(NSArray *)otherArray;

// 修改数组
- (void)setArray:(NSArray *)otherArray;

// 替换指定索引的元素
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;

// 交换数组元素
- (void)exchangeObjectAtIndex:(NSUInteger)index withObjectAtIndex:(NSUInteger)idx2;
上一篇 下一篇

猜你喜欢

热点阅读