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;