IOS 之 NSArray 数组方法详解

NSArray 数组的创建
1、使用类方法创建 一个空的数组
+ (id)array;


2、使用类方法创建 只有一个对象的数组
+ (id)arrayWithObject:(id)anObject;


3、从 c 数组创建一个 NSarray  数以cnt 不能超出数组的范围。不然会有数据越界的异常
+ (id)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt;


id objects[10] = {@"abbb",@"bczdfasdf",@"casdfasdf",@"asdfasdf"};

    NSArray *array = [NSArray arrayWithObjects:objects count:2];
    
    NSLog(@"%@",array);


4、  使用后面的元素,创建一个数组
+ (id)arrayWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;


5、array 创建一个新的数组
+ (id)arrayWithArray:(NSArray *)array;


6、使用 c 数组 创建一个数组。 
- (id)initWithObjects:(const id [])objects count:(NSUInteger)cnt;


7、使用objects 创建数组
- (id)initWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;


8、使用一个array 创建一个数组
- (id)initWithArray:(NSArray *)array;


9、使用array 创建一个数组,后面的标识是 是否拷贝原来的元素
flag 如果是YES, 数组中每个元素,将引用copywithzone。
- (id)initWithArray:(NSArray *)array copyItems:(BOOL)flag;


10、读取文件创建一个数组,
+ (id)arrayWithContentsOfFile:(NSString *)path;


11、使用URL 穿件一个数组,这个URL可以是本地的文件路径,也可是是网络上的内容
+ (id)arrayWithContentsOfURL:(NSURL *)url;


12、读取文件创建一个数组,
- (id)initWithContentsOfFile:(NSString *)path;


13、使用URL 穿件一个数组,这个URL可以是本地的文件路径,也可是是网络上的内容
- (id)initWithContentsOfURL:(NSURL *)url;

1、 向数组中添加一个对象
- (void)addObject:(id)anObject;


2、向数组中指定的index 位置,插入一个新的对象
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;


3、移除数组的最后一个元素
- (void)removeLastObject;


4、移除指定为指定位置的元素
- (void)removeObjectAtIndex:(NSUInteger)index;


5、使用anObject 替换 下标为 index 位置上的元素
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;


6、使用一个数组给当前的数组添加元素
- (void)addObjectsFromArray:(NSArray *)otherArray;


7、交换指定 index1 和 index2 两个位置上的元素
- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;


8、 移除数组中的所有元素
- (void)removeAllObjects;


9、使用anObject 对象替换 range 位置上的元素,
相当于删除 range位置的元素,然后在把 anobject 插入到这个位置
- (void)removeObject:(id)anObject inRange:(NSRange)range;


10、如果指定的元素,如果元素不存在,则不移除
- (void)removeObject:(id)anObject;


11、 同9 相同
- (void)removeObjectIdenticalTo:(id)anObject inRange:(NSRange)range;


12、方法内容 和9 相同
- (void)removeObjectIdenticalTo:(id)anObject;


13、不建议使用
- (void)removeObjectsFromIndices:(NSUInteger *)indices numIndices:(NSUInteger)cnt NS_DEPRECATED(10_0, 10_6, 2_0, 4_0);


14、移除给定数组中的元素
- (void)removeObjectsInArray:(NSArray *)otherArray;


15、移除指定range  上的所有元素 
- (void)removeObjectsInRange:(NSRange)range;


16、使用otherArray 数组中 otherRange 位置上的元素,替换当前数组中 range 位置上的元素
- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray range:(NSRange)otherRange;


17 、 使用otherArray 数组上的位置,替换 range 上的元素
- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray;


18、对当前的数组排序,使用排序算法
- (void)sortUsingFunction:(NSInteger (*)(id, id, void *))compare context:(void *)context;


19、对当前的数组排序,使用排序算法
- (void)sortUsingSelector:(SEL)comparator;


20、在indexes 的位置上,插入一个数组
- (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes;


21、移除制定indexes 位置上的元素
- (void)removeObjectsAtIndexes:(NSIndexSet *)indexes;


22、使用一个对象数组,替换 indexes 位置上的 元素
- (void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray *)objects;


23
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0);


24、排序
- (void)sortUsingComparator:(NSComparator)cmptr
25、 使用后面的元素进行排序
- (void)sortWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr


25、 创建NSMutableArray 数组
+ (id)arrayWithCapacity:(NSUInteger)numItems;
- (id)initWithCapacity:(NSUInteger)numItems;

转载于:https://my.oschina.net/wangbin618/blog/489569

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值