NSArray全部API学习。
返回数组指定下标的元素
- ()objectAtIndex:(NSUInteger)index;
初始化数组
instancetype)init NS_DESIGNATED_INITIALIZER
初始化数组并且赋初值以及大小
- (instancetype)initWithObjects:(const [])objects count:(NSUInteger)cnt NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
@interface NSArray (NSExtendedArray)
- (NSArray *)arrayByAddingObject:()anObject;
追加其他数组返回新的数组
- (NSArray *)arrayByAddingObjectsFromArray:(NSArray *)otherArray;
分割数组为字符串
- (NSString *)componentsJoinedByString:(NSString *)separator;
判断数组是否存在指定元素
- ()containsObject:()anObject;
格式化属性列表
@propertyreadonlycopyNSString *description;
//10.本地格式化为一个属性列表
- (NSString *)descriptionWithLocale:()locale;
//11.本地格式化为一个属性列表,并且可以设置缩进
- (NSString *)descriptionWithLocale:()locale indent:(NSUInteger)level;
//12.arraydemo对象第一个交集元素
- ()firstObjectCommonWithArray:(NSArray *)otherArray;
//13.返回指定区域的对象
- ()getObjects:( __unsafe_unretained [])objects range:(NSRange)range;
//14.获取指定元素的索引
- (NSUInteger)indexOfObject:()anObject;
//15.指定区域获取元素的索引
- (NSUInteger)indexOfObject:()anObject inRange:(NSRange)range;
//16.获取指定元素的索引
- (NSUInteger)indexOfObjectIdenticalTo:()anObject;
//17.指定区域获取元素的索引
- (NSUInteger):()anObject inRange:(NSRange)range;
//18.判断两个数组对象是否相等
- ()isEqualToArray:(NSArray *)otherArray;
//19.第一个对象属性
@property (nonatomic, readonly) firstObject NS_AVAILABLE(_6, _0);
//20.最后一个对象元素属性
@propertynonatomicreadonlyid lastObject;
//21.返回一个枚举对象
- (NSEnumerator *)objectEnumerator;
//22.返回一个翻转的枚举对象
- (NSEnumerator *)reverseObjectEnumerator;
//23.升序排序数组元素
@property (readonly, ) NSData *sortedArrayHint;
//24.调用指定方法排序数组元素
- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(, , *))comparator context:( *)context;
//25.调用指定方法排序数组元素
- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(, , *))comparator context:( *)context hint:(NSData *)hint;
//26.指定比较方法排序数组元素
- (NSArray *)sortedArrayUsingSelector:()comparator;
//27.返回指定区域的数组元素组成新的数组元素
- (NSArray *)subarrayWithRange:(NSRange)range;
//28.保存数组对象到指定文件
- ()writeToFile:(NSString *)path atomically:()useAuxiliaryFile;
//29.保存数组对象到指定URL
- ()writeToURL:(NSURL *)url atomically:()atomically;
//30.每个数组元素都调用指定方法
- ()makeObjectsPerformSelector:()aSelector;
//31.每个数组元素都调用指定方法
- ()makeObjectsPerformSelector:()aSelector withObject:()argument;
//33.指定索引集合返回新数组
- (NSArray *)objectsAtIndexes:(NSIndexSet *)indexes;
//34.返回指定元素数组元素
- ()objectAtIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(_8, _0);
//35.数组中的每个元素都执行代码快中方法遍历
- ()enumerateObjectsUsingBlock:( (^)( obj, NSUInteger idx, *stop))block NS_AVAILABLE(_6, _0);
//36.指定块使数组每个元素都执行代码块中的代码
- ()enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:( (^)( obj, NSUInteger idx, *stop))block NS_AVAILABLE(_6, _0);
//37.遍历数组每个元素执行代码块中的方法
- ()enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:( (^)( obj, NSUInteger idx, *stop))block NS_AVAILABLE(_6, _0);
//38.根据条件来获取一个NSUIndex对象,主要是根据条件进行数据遍历使用
- (NSUInteger)indexOfObjectPassingTest:( (^)( obj, NSUInteger idx, *stop))predicate NS_AVAILABLE(_6, _0);
NSArray *array = [NSArrayarrayWithObjects@"wendy"@"andy"@"tom"@"test"
NSInteger index = [array indexOfObjectPassingTest:^ ( tr,NSUInteger index, *te){
NSString *s = (NSString *)tr;
([@"wendy" isEqualToString:s])
return ;
return ;
NSLog(@"index==%ld=.",()index);
2015-06-09 15:44:01.982 NSArray[5064:145993] index==0=.
//39.同上面的方法相同,区别在于,这里添加了一个参数,用来表示遍历是从前向后遍历还是从后向前遍历
- (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts passingTest:( (^)( obj, NSUInteger idx, *stop))predicate NS_AVAILABLE(_6, _0);
//40.这个添加了参数NSIntexSet参数,用来获取子数组,然后使用子数组进行遍历,处理数据
- (NSUInteger)indexOfObjectAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:( (^)( obj, NSUInteger idx, *stop))predicate NS_AVAILABLE(_6, _0);
//41.根据block的处理获取一个NSIndexSet对象
- (NSIndexSet *)indexesOfObjectsPassingTest:( (^)( obj, NSUInteger idx, *stop))predicate NS_AVAILABLE(_6, _0);
NSIndexSet *index = [array indexesOfObjectsPassingTest: ^ ( tr, NSUInteger index, *te){
NSString *s = (NSString *)tr;
([s isEqualToString:@"andy"]){
return ;
return ;
NSLog(@"%@",index);
2015-06-09 15:50:40.946 NSArray[5128:150086] [number of indexes: 1 (in 1 ranges), indexes: (1)]
//42.这个方法添加了参数,用来表示是从前向后遍历还是从后向前遍历
- (NSIndexSet *)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:( (^)( obj, NSUInteger idx, *stop))predicate NS_AVAILABLE(_6, _0);
NSIndexSet * index = [array indexesOfObjectsWithOptionsNSEnumerationReversepassingTestNSUInteger index, *te){
NSString *s = (NSString *)tr;
([s isEqualToString:@"andy"]){
return ;
return ;
NSLog(@"%@",index);
2015-06-09 16:18:53.974 NSArray[5283:158602] [number of indexes: 1 (in 1 ranges), indexes: (1)]
//43.添加参数NSIndexSet用来获取子数组,使用子数组进行遍历
- (NSIndexSet *)indexesOfObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:( (^)( obj, NSUInteger idx, *stop))predicate NS_AVAILABLE(_6, _0);
//44.对数组进行排序操作,参数cmptr是一个block函数块,返回的数据类型是一个NSComparisonResult对象
- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr NS_AVAILABLE(_6, _0);
//45.进行排序操作,NSSortOptions排序的参数用来表示是同时排序,还是稳定排序
- (NSArray *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr NS_AVAILABLE(_6, _0);
NSArray *test = [array sortedArrayWithOptionsNSSortStableusingComparatorNSComparisonResultNSStringNSString *s2){
(s.length < s2.length){
returnNSOrderedAscending
(s.length > s2.length){
returnNSOrderedDescending
returnNSOrderedSame
NSLog(@"%@",test);
2015-06-09 16:24:34.498 NSArray[5332:161773] (
tom,
andy,
test,
wendy
)
typedef NS_OPTIONS(NSUInteger, NSBinarySearchingOptions) {
NSBinarySearchingFirstEqual = (UL << ),
NSBinarySearchingLastEqual = (UL << ),
NSBinarySearchingInsertionIndex = (UL << ),
//46.指定区域返回通过代码块方法的索引
- (NSUInteger)indexOfObject:()obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator)cmp NS_AVAILABLE(_6, _0); // binary search
@interface NSArray (NSArrayCreation)
//47.快速创建一个空数组
instancetype)array;
//48.指定一个元素创建数组对象
+ (instancetype)arrayWithObject:()anObject;
//49.指定个数从一个数组对象创建新的数组对象
+ (instancetype)arrayWithObjects:(const [])objects count:(NSUInteger)cnt;
//50.指定多个元素创建数组对象
+ (instancetype)arrayWithObjects:()firstObj, ... NS_REQUIRES_NIL_TERMINATION;
//51.生成新的另外一个数组
+ (instancetype)arrayWithArray:(NSArray *)array;
//52.指定个数从一个数组对象创建新的数组对象
- (instancetype)initWithObjects:()firstObj, ... NS_REQUIRES_NIL_TERMINATION;
//53.从已有数组初始化
- (instancetype)initWithArray:(NSArray *)array;
//54.从已有数组初始化(标示作用,潜复制,还是深复制)
- (instancetype)initWithArray:(NSArray *)array copyItems:()flag;
//55.从文件加载数组元素
+ (NSArray *)arrayWithContentsOfFile:(NSString *)path;
//56.从网络地址加载数组元素
+ (NSArray *)arrayWithContentsOfURL:(NSURL *)url;
//57.从文件加载数据
- (NSArray *)initWithContentsOfFile:(NSString *)path;
//58.从文件加载数据
- (NSArray *)initWithContentsOfURL:(NSURL *)url;
/****************Mutable Array****************/
@interface NSMutableArray : NSArray
//59.
- ()addObject:()anObject;
//60.指定索引位置插入对象
- ()insertObject:()anObject atIndex:(NSUInteger)index;
//61.移除最后一个对象
- ()removeLastObject;
//62.移除指定索引位置对象
- ()removeObjectAtIndex:(NSUInteger)index;
//63.替换指定索引位置对象
- ()replaceObjectAtIndex:(NSUInteger)index withObject:()anObject;
//64.初始化数组
instancetype)init NS_DESIGNATED_INITIALIZER
//65.初始化数组并且指定大小
- (instancetype)initWithCapacity:(NSUInteger)numItems NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
@interface NSMutableArray (NSExtendedMutableArray)
//66.追加新数组A到指定数组B后面
- ()addObjectsFromArray:(NSArray *)otherArray;
//67.交换指定索引之间的对象
- ()exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;
//68.移除所有的数组数据
- ()removeAllObjects;
//69.移除知道那个区域指定对象
- ()removeObject:()anObject inRange:(NSRange)range;
//70.移除指定对象
- ()removeObject:()anObject;
//71.移除指定区域指定对象
- ()removeObjectIdenticalTo:()anObject inRange:(NSRange)range;
//72.移除指定对象
- ()removeObjectIdenticalTo:()anObject;
//73.移除指定索引之间的对象
- ()removeObjectsFromIndices:(NSUInteger *)indices numIndices:(NSUInteger)cnt NS_DEPRECATED(_0, _6, _0, _0);
//74.移除数组包含的指定数组的元素
- ()removeObjectsInArray:(NSArray *)otherArray;
//75.移除指定区域所有对象
- ()removeObjectsInRange:(NSRange)range;
//76.指定数组的指定区域的元素替换为数组指定区域元素
- ()replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray range:(NSRange)otherRange;
//77.指定数组替换数组指定区域的元素
- ()replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray;
//78.AB
- ()setArray:(NSArray *)otherArray;
//79.指定方法升序比较
- ()sortUsingFunction:(NSInteger (*)(, , *))compare context:( *)context;
//80.指定方法升序比较
- ()sortUsingSelector:()comparator;
//81.指定索引集合插入数组元素
- ()insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes;
//82.移除指定索引集合元素
- ()removeObjectsAtIndexes:(NSIndexSet *)indexes;
//83.替换指定索引集合的数组元素
- ()replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray *)objects;
//84.指定索引替换新的数组元素
- ()setObject:()obj atIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(_8, _0);
//85.指定块方法排序数组元素
- ()sortUsingComparator:(NSComparator)cmptr NS_AVAILABLE(_6, _0);
//86.指定块比较数组
- ()sortWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr NS_AVAILABLE(_6, _0);
@interface NSMutableArray (NSMutableArrayCreation)
//87.初始化数组并指定对象容量
+ (instancetype)arrayWithCapacity:(NSUInteger)numItems;
//88.指定路径文件加载数据
+ (NSMutableArray *)arrayWithContentsOfFile:(NSString *)path;
//89.指定URL加载数据
+ (NSMutableArray *)arrayWithContentsOfURL:(NSURL *)url;
//90.指定路径文件加载数据
- (NSMutableArray *)initWithContentsOfFile:(NSString *)path;
//91.指定URL加载数据
- (NSMutableArray *)initWithContentsOfURL:(NSURL *)url;