/** arrayByAddingObject -- 往数组A添加一个数据,返回一个数组的形式,即必须有一个数组来接受 */ NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3", nil]; array = [array arrayByAddingObject:@"4"]; NSLog(@"-----addarr=%@-----", array); //>>1,2,3,4 /** arrayByAddingObjectsFromArray -- 往数组A添加一组数组B,返回数组类型,需要一个数组来接受数据 */ NSArray *addArr2 = [array arrayByAddingObjectsFromArray:array]; NSLog(@"-----addArr2=%@-----", addArr2); //>>>1,2,3,4,1,2,3,4 /** componentsJoinedByString -- 将数据A以 需要的分隔符分割开,组成一个字符串:例如数组A= [1,2,3];分隔符为^, 字符串为1^2^3 */ NSString *arrayStr = [array componentsJoinedByString:@"^"]; NSLog(@"-----arrayStr=%@-----", arrayStr); //>>>1^2^3 /** containsObject -- 数组是否包含某个对象 */ BOOL isContains = [array containsObject:@"5"]; NSLog(@"-----isContains=%d-----", isContains); //>>>NO /** description */ NSString *arrDes = [array description]; NSLog(@"-----description=%@-----", arrDes); /** indexOfObject --判断 anObject 对象是否存在数组中如果存在返回,对象所在的下标;如果不存在,返回NSNotFund */ NSUInteger index = [addArr2 indexOfObject:@"5"]; NSLog(@"-----index=%ld-----", index); /** indexOfObjectIdenticalTo: inRange: -- 判断 anObject 对象是否存在数组中range范围内,如果存在,返回下标了;如果不存在,返回NSNotFund */ NSUInteger index2 = [addArr2 indexOfObject:@"3" inRange:NSMakeRange(1, 3)]; NSLog(@"-----index2 = %ld-----", index2); // 2 /** objectEnumerator --正序遍历数组 reverseObjectEnumerator --反序遍历数组 */ NSEnumerator *rator = [array objectEnumerator]; id obj = nil; while (obj = [rator nextObject]) { NSLog(@"----obj=-%@-----",obj); } /** isEqualToArray -- 两个数组是否相等 */ BOOL isSame = [addArr2 isEqualToArray:array]; NSLog(@"-----isSame = %d-----", isSame); /** sortedArrayUsingSelector -- 如果只是对字符串排序的话, 可以利用 字符串自带的compare:方法;也可以自己写compare:方法,进行对象的比较 */ /** subarrayWithRange -- 截取数组 range的数组 */ NSArray *subArray = [addArr2 subarrayWithRange:NSMakeRange(2, 4)]; NSLog(@"-----subArray=%@-----", subArray); /** writeToFile: atomically: -- 把数据写入本地 */ /** writeToURL: atomically: --保存数组至一个URL */ /** makeObjectsPerformSelector:(SEL)aSelector -- 让每个数组都调用 aSelector 这个方法 */ NSLog(@"-----addArr2=%@-----", addArr2); /** objectAtIndexedSubscript: -- 获取数组中 idx 索引的数据 */ NSString *strIndex = [addArr2 objectAtIndexedSubscript:3]; NSLog(@"-----strIndex=%@-----", strIndex); //>>>4 /** enumerateObjectsUsingBlock: -- 这个方法也是一种循环的方法与for,while实现的功能一样;当stop = yes的时候,就停止循环 */ [addArr2 enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { // NSLog(@"-----obj= %@ idx = %ld -----", obj, idx); }]; /** enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock: -- 这个方法也是一种循环的方法与for,while实现的功能一样;当stop = yes的时候,就停止循环 */ //NSEnumerationReverse 倒序遍历 [array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL * _Nonnull stop) { NSLog(@"-----obj= %@ idx = %ld -----", obj, idx); //当需要结束循环的时候,调用stop,赋予YES // if (idx ==3) { // *stop = YES; // } }]; /** enumerateObjectsAtIndexes:(NSIndexSet *)s options: -- 在制定的数组范围 s 内 遍历数据 */ [addArr2 enumerateObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 4)] options:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSLog(@"-----obj= %@ idx = %ld -----", obj, idx); }]; /** indexOfObjectPassingTest -- 根据条件用来获取一个NSUIndex 对象,主要是根据条件进行数据遍历使用 */ NSInteger testIndex = [array indexOfObjectPassingTest:^ BOOL (id tr,NSUInteger index, BOOL *te){ NSString *s = (NSString *)tr; if([@"4" isEqualToString:s]) { return YES; } return NO; }]; NSLog(@"index==%ld=.",testIndex); //>>> 3 /** sortedArrayUsingComparator: -- 排序 , 这个方法本身就是按递增的方式排序。 NSOrderedAscending -The left operand is smaller than the right operand. NSOrderedSame -The two operands are equal. NSOrderedDescending -The left operand is greater than the right operand */ NSArray *numberArray = @[@"3", @"4", @"9", @"1"]; //递增 NSArray *sortArray = [numberArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { if ([obj1 integerValue] > [obj2 integerValue]) { return NSOrderedDescending; }else { return NSOrderedAscending; } }]; NSLog(@"-----sortArray=%@-----", sortArray); //>>>1 3 4 9 /** sortedArrayWithOptions:(NSSortOptions)opts usingComparator -- 与上个方法一致 */
此外还提供了NAMutableArray的方法,这个方法常用,都知道意思,只列举方法:
@interface NSMutableArray<ObjectType> : NSArray<ObjectType> - (void)addObject:(ObjectType)anObject; --往数组添加数据 - (void)insertObject:(ObjectType)anObject atIndex:(NSUInteger)index; --往数组的第index下标插入一个anobject数据 - (void)removeLastObject; -- 移除数组的最后一个数据 - (void)removeObjectAtIndex:(NSUInteger)index; --移除数组中第index个数据 - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(ObjectType)anObject; --用 anobject 替换 数组 中 第index个数据 - (instancetype)init NS_DESIGNATED_INITIALIZER; - (instancetype)initWithCapacity:(NSUInteger)numItems NS_DESIGNATED_INITIALIZER; - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER; @end @interface NSMutableArray<ObjectType> (NSExtendedMutableArray) - (void)addObjectsFromArray:(NSArray<ObjectType> *)otherArray; - (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2; - (void)removeAllObjects; - (void)removeObject:(ObjectType)anObject inRange:(NSRange)range; - (void)removeObject:(ObjectType)anObject; - (void)removeObjectIdenticalTo:(ObjectType)anObject inRange:(NSRange)range; - (void)removeObjectIdenticalTo:(ObjectType)anObject; - (void)removeObjectsFromIndices:(NSUInteger *)indices numIndices:(NSUInteger)cnt NS_DEPRECATED(10_0, 10_6, 2_0, 4_0); - (void)removeObjectsInArray:(NSArray<ObjectType> *)otherArray; - (void)removeObjectsInRange:(NSRange)range; - (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray<ObjectType> *)otherArray range:(NSRange)otherRange; - (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray<ObjectType> *)otherArray; - (void)setArray:(NSArray<ObjectType> *)otherArray; - (void)sortUsingFunction:(NSInteger (NS_NOESCAPE *)(ObjectType, ObjectType, void * _Nullable))compare context:(nullable void *)context; - (void)sortUsingSelector:(SEL)comparator; - (void)insertObjects:(NSArray<ObjectType> *)objects atIndexes:(NSIndexSet *)indexes; - (void)removeObjectsAtIndexes:(NSIndexSet *)indexes; - (void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray<ObjectType> *)objects; - (void)setObject:(ObjectType)obj atIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0); - (void)sortUsingComparator:(NSComparator NS_NOESCAPE)cmptr NS_AVAILABLE(10_6, 4_0); - (void)sortWithOptions:(NSSortOptions)opts usingComparator:(NSComparator NS_NOESCAPE)cmptr NS_AVAILABLE(10_6, 4_0); @end @interface NSMutableArray<ObjectType> (NSMutableArrayCreation) + (instancetype)arrayWithCapacity:(NSUInteger)numItems; + (nullable NSMutableArray<ObjectType> *)arrayWithContentsOfFile:(NSString *)path; + (nullable NSMutableArray<ObjectType> *)arrayWithContentsOfURL:(NSURL *)url; - (nullable NSMutableArray<ObjectType> *)initWithContentsOfFile:(NSString *)path; - (nullable NSMutableArray<ObjectType> *)initWithContentsOfURL:(NSURL *)url;