美文网首页
Objective-C 学习笔记 - 数组与可变数组

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

作者: 遠遊旳遊子 | 来源:发表于2018-08-27 14:48 被阅读0次

【前言】

  • OC中使用两种数组,第一种是OC数组,第二种是C语言的数组,使用OC数组存放对象类型,使用C语言数组存放基础数据类型
  • OC数组也是一个类,我们使用的数组也是一个对象
  • OC数组存放的是对象的指针,一个对象可以存放到多个数组中,通过一个数组中得指针改变了对象的内容,其他数组中得指针读取对象也会发生相应的变化

关键词:NSMutableArray : NSArray

  • 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;
  • NSMutableArray

<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;

相关文章

网友评论

      本文标题:Objective-C 学习笔记 - 数组与可变数组

      本文链接:https://www.haomeiwen.com/subject/chzziftx.html