IOS 之 NSArray 数组方法详解(4)

这篇博客详细介绍了NSArray的各种操作方法,包括添加对象、在指定位置插入和移除对象、替换元素、数组排序以及批量操作等,帮助iOS开发者更好地理解和使用NSArray。
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;
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值