从属关系:NSMutableArray:NSArray:NSObject :Foundation
数组NSArray
、NSMutableArray
(可变数组、继承自NSArray) 是OC中的数组类
(1)只能存放任意OC对象, 并且是有顺序的
(2)不能存储非OC对象, 比如int\float\double\char\enum\struct等
(3)NSArray中不能存储nil,因为NSArray认为nil是数组的结束(nil是数组元素结束的标记)。nil就是0。0也是基本数据类型,不能存放到NSArray中。
一、数组NSArray
二、可变数组NSMutableArray
//1.初始化
// 创建一个空数组
NSArray *arr1 = [NSArray array];
// 调用对象方法,创建数组
NSArray *arr2 = [[NSArray alloc] initWithObjects:@"three",[NSNull null],@"four", nil];
// 创建数组,有多个元素 nil 表示数组赋值结束 常见写法
NSArray *arr3 = [NSArray arrayWithObjects:@"one",@"two",@1, nil];
// 用一个数组可以创建另外一个数组
NSArray *arr4 = [NSArray arrayWithArray:arr3];
NSLog(@"arr1 = %@",arr1);
NSLog(@"arr2 = %@",arr2);
NSLog(@"arr3 = %@",arr3);
NSLog(@"arr4 = %@",arr4);
//NSArray简化形式
NSArray *arr5 = @[@"1",@"one",@"3",@4,@"ONE"];
NSLog(@"arr5 = %@",arr5);
执行结果:
2019-05-21 17:57:17.292118+0800 MGNSArray[704:68468] arr1 = (
)
2019-05-21 17:57:17.292256+0800 MGNSArray[704:68468] arr2 = (
three,
“”,
four
)
2019-05-21 17:57:17.292369+0800 MGNSArray[704:68468] arr3 = (
one,
two,
1
)
2019-05-21 17:57:17.292407+0800 MGNSArray[704:68468] arr4 = (
one,
two,
1
)
2019-05-21 17:57:17.292468+0800 MGNSArray[704:68468] arr5 = (
1,
one,
3,
4,
ONE
)
//2.数组访问及数组长度
NSArray *arr6 = [NSArray arrayWithObjects:@"one",@"two",@1,@"three", nil];
//访问数组元素
NSString *str1 = [arr6 objectAtIndex:1];
//或简化
NSString *str2 = arr6[1];
NSLog(@"str1 = %@",str1);
NSLog(@"str2 = %@",str2);
//获取数组的长度(元素的个数)
NSLog(@"%ld",arr6.count);
执行结果:
2019-05-21 17:57:17.292484+0800 MGNSArray[704:68468] str1 = two
2019-05-21 17:57:17.292497+0800 MGNSArray[704:68468] str2 = two
2019-05-21 17:57:17.292508+0800 MGNSArray[704:68468] 4
//3.数组查找
//根据下标,获取下标对应的对象 从0开始
NSArray *arr7 = [NSArray arrayWithObjects:@"one",@"two",@1,@"three", nil];
NSLog(@"%@",[arr7 objectAtIndex:3]);
//元素返回元素的下标
NSUInteger index1 = [arr7 indexOfObject:@1];
NSLog(@"index1 = %ld",index1);
//在range范围内查找anObject元素在数组中的位置
NSLog(@"元素在数组的位置 %lu",[arr6 indexOfObject:@"three" inRange:NSMakeRange(1, 3)]);
//数组中是否包含了某个元素
if([arr7 containsObject:@"four"]){
NSLog(@"包含此元素");
} else {
NSLog(@"不包含此元素");
}
执行结果:
2019-05-21 17:57:17.292521+0800 MGNSArray[704:68468] three
2019-05-21 17:57:17.292541+0800 MGNSArray[704:68468] index1 = 2
2019-05-21 17:57:17.292611+0800 MGNSArray[704:68468] 元素在数组的位置 3
2019-05-21 17:57:17.292630+0800 MGNSArray[704:68468] 不包含此元素
//4.始末元素
NSArray *arr8 = @[@"ab",@"cd",@"12",@34];
NSLog(@"开始一个元素 = %@",[arr8 firstObject]);
NSLog(@"最后一个元素 = %@",[arr8 lastObject]);
执行结果:
2019-05-21 17:57:17.292652+0800 MGNSArray[704:68468] 开始一个元素 = ab
2019-05-21 17:57:17.292686+0800 MGNSArray[704:68468] 最后一个元素 = 34
//5.数组中插入数据,返回新的数组
NSArray *arr9 = @[@"ab",@"cd",@"12",@"34",@34];
NSArray *arr10 = [arr9 arrayByAddingObject:@"4"];
NSLog(@"插入数据arr10 = %@",arr10);
执行结果:
2019-05-21 18:03:50.972845+0800 MGNSArray[707:69398] 插入数据arr10 = (
ab,
cd,
12,
34,
34,
4
)
//6.数组数据连接、将字符串拆成数组
NSArray *arr11 = @[@"ab",@"cd",@"12",@"34",@34];
NSString *str3 = [arr11 componentsJoinedByString:@","];
NSLog(@"数组字符连接str3 = %@",str3);
//将字符串拆成数组
NSArray *arr12 = [@"1,2,3,4,5" componentsSeparatedByString:@","];
NSLog(@"将字符串拆成数组arr12 = %@",arr12);
执行结果:
2019-05-21 17:57:17.292817+0800 MGNSArray[704:68468] 数组字符连接str3 = ab,cd,12,34,34
2019-05-21 17:57:17.292878+0800 MGNSArray[704:68468] 将字符串拆成数组arr12 = (
1,
2,
3,
4,
5
)
//7.文件的读写(plist文件与xml文件)
NSArray *arr13 = @[@"爱国", @"敬业", @"和谐"];
//这个方法获取出的结果是一个数组.因为有可以搜索到多个路径.
NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
//在这里,我们指定搜索的是Cache目录,所以结果只有一个,取出Cache目录
NSString *cachePath = array[0];
NSLog(@"%@",cachePath);
//拼接文件路径
NSString *filePathName1 = [cachePath stringByAppendingPathComponent:@"arr.plist"];
NSString *filePathName2 = [cachePath stringByAppendingPathComponent:@"arr.xml"];
//将数组中的内容写入文件
BOOL isOK1 = [arr13 writeToFile:filePathName1 atomically:YES];
BOOL isOK2 = [arr13 writeToFile:filePathName2 atomically:YES];
NSLog(@"isOK1 = %d|isOK2 = %d",isOK1,isOK2);
//从文件中读取数据到数组
NSArray *arr14 = [NSArray arrayWithContentsOfFile:filePathName1];
NSArray *arr15 = [NSArray arrayWithContentsOfFile:filePathName2];
NSLog(@"arr14 = %@|arr15 = %@",arr14,arr15);
执行结果:
2019-05-21 17:57:17.293093+0800 MGNSArray[704:68468] /var/mobile/Containers/Data/Application/0566ACC2-2AF1-4461-93EE-3949E17C1388/Library/Caches
2019-05-21 17:57:17.313724+0800 MGNSArray[704:68468] isOK1 = 1|isOK2 = 1
2019-05-21 17:57:17.314295+0800 MGNSArray[704:68468] arr14 = (
“\U7231\U56fd”,
“\U656c\U4e1a”,
“\U548c\U8c10”
)|arr15 = (
“\U7231\U56fd”,
“\U656c\U4e1a”,
“\U548c\U8c10”
)
//8.数组的遍历
NSArray *arr16 = @[@"ab",@"cd",@"12",@"34"];
//[1]使用 for 循环遍历
for (int i = 0; i < arr16.count; i++) {
NSLog(@"%@",[arr16 objectAtIndex:i]);//NSLog(@"%@",arr16[i]);
}
//[2]使用 for-in 循环遍历(快速枚举法) 要求数据类型一致
for (NSString *str in arr16) {
NSLog(@"%@",str);
}
//[3]使用 block 遍历
//idx:循环索引下标(0开始)
//obj:当前对象
//stop:YES 会停止, stop:NO 不会停止
[arr16 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL * stop) {
NSLog(@"%lu----%@",idx,obj);
}];
执行结果:
2019-05-21 17:57:17.314327+0800 MGNSArray[704:68468] ab
2019-05-21 17:57:17.314356+0800 MGNSArray[704:68468] cd
2019-05-21 17:57:17.314367+0800 MGNSArray[704:68468] 12
2019-05-21 17:57:17.314378+0800 MGNSArray[704:68468] 34
2019-05-21 17:57:17.314411+0800 MGNSArray[704:68468] ab
2019-05-21 17:57:17.314423+0800 MGNSArray[704:68468] cd
2019-05-21 17:57:17.314434+0800 MGNSArray[704:68468] 12
2019-05-21 17:57:17.314444+0800 MGNSArray[704:68468] 34
2019-05-21 17:57:17.314459+0800 MGNSArray[704:68468] 0----ab
2019-05-21 17:57:17.314472+0800 MGNSArray[704:68468] 1----cd
2019-05-21 17:57:17.314484+0800 MGNSArray[704:68468] 2----12
2019-05-21 17:57:17.314496+0800 MGNSArray[704:68468] 3----34
//二、可变数组NSMutableArray
//1、实例化:同理用Capacity初始化,也有两种实例化方法
//初始化可变数组对象的长度,如果后面代码继续添加数组超过长度10以后NSMutableArray的长度会自动扩充
NSMutableArray *mtArr1 = [NSMutableArray arrayWithCapacity:10];
NSMutableArray *mtArr2 = [[NSMutableArray alloc] initWithCapacity:10];
NSMutableArray *mtArr3 = [[NSMutableArray alloc] initWithObjects:@"a",@"b",@"c",@"d", nil];
NSLog(@"mtArr1 = %@",mtArr1);
NSLog(@"mtArr2 = %@",mtArr2);
NSLog(@"mtArr3 = %@",mtArr3);
//增
[mtArr1 addObject:@"AA"];
[mtArr1 addObject:@"BB"];
[mtArr1 insertObject:@"X"atIndex:1];//插入
NSLog(@"增mtArr1 = %@",mtArr1);
//删
//删除最后一个对象
[mtArr1 removeLastObject];
//删除特定位置(下标)的对象
[mtArr1 removeObjectAtIndex:0];
//删除特定对象(只要内同为@"X”,全部删除)
[mtArr1 removeObject:@"X"];
//全删
[mtArr1 removeAllObjects];
NSLog(@"删mtArr1 = %@",mtArr1);
/*
[mtArr1 removeObject:@"AA" inRange:NSMakeRange(1, 2)];//删除指定范围中的指定对象
[mtArr1 removeObjectsInRange:NSMakeRange(2, 2)];//删除指定范围中得元素
[mtArr1 removeObjectIdenticalTo:@"AA"];//删除数组中指定元素,根据对象的地址判断
[mtArr1 removeObjectIdenticalTo:@"AA" inRange:NSMakeRange(2, 1)];//在指定范围内删除指定的元素。
*/
//如果要删除不连续的元素,则先把要删除的元素放在一个清单中,然后再删除该清单
//比如要删下标为偶数的
[mtArr1 addObject:@"AA"];
[mtArr1 addObject:@"BB"];
NSMutableIndexSet *indexs = [NSMutableIndexSet indexSet];
for (int i = 0; i < [mtArr1 count]; i++) {
if (i % 2 == 0) {
[indexs addIndex:i];
}
}
//删除清单中得元素
[mtArr1 removeObjectsAtIndexes:indexs];
NSLog(@"删%@",mtArr1);
//改
[mtArr2 addObject:@"AA"];
[mtArr2 addObject:@"BB"];
NSLog(@"mtArr2 = %@",mtArr2);
//按索引改
[mtArr2 replaceObjectAtIndex:0 withObject:@"DD"];
NSLog(@"mtArr2 = %@",mtArr2);
//可变数组的交换,方法只能用在可变数组
[mtArr2 exchangeObjectAtIndex:0 withObjectAtIndex:1];
NSLog(@"mtArr2 = %@",mtArr2);
//在指定范围内,修改数组中得内容。
NSArray *arr17 = @[@"EEEE",@"FF",@"GG"];
//[arr13 replaceObjectsAtIndexes:indexs withObjects:arr14];
[mtArr2 replaceObjectsInRange:NSMakeRange(0, 2) withObjectsFromArray:arr17];
NSLog(@"mtArr2 = %@",mtArr2);
//用OC对C语言中得数组进行排序(冒泡排序)
NSArray *arr18 = @[[NSNumber numberWithInt:10],[NSNumber numberWithInt:1],[NSNumber numberWithInt:3],[NSNumber numberWithInt:12],[NSNumber numberWithInt:22],[NSNumber numberWithInt:5],[NSNumber numberWithInt:33]];
NSMutableArray *mtArr4 = [[NSMutableArray alloc]initWithArray:arr18];
NSLog(@"排序前:%@",mtArr4);
for (int i = 0; i < mtArr4.count; i++) {
for (int j = 0; j < mtArr4.count - i - 1;j++) {
if ([mtArr4[j+1] integerValue] < [mtArr4[j] integerValue]) {
NSInteger temp = [mtArr4[j] integerValue];
mtArr4[j] = mtArr4[j + 1];
mtArr4[j + 1] = [NSNumber numberWithInteger:temp];
}
}
}
NSLog(@"冒泡排序后:%@",mtArr4);
执行结果:
2019-05-21 17:57:17.314532+0800 MGNSArray[704:68468] mtArr1 = (
)
2019-05-21 17:57:17.314558+0800 MGNSArray[704:68468] mtArr2 = (
)
2019-05-21 17:57:17.314590+0800 MGNSArray[704:68468] mtArr3 = (
a,
b,
c,
d
)
2019-05-21 17:57:17.314640+0800 MGNSArray[704:68468] 增mtArr1 = (
AA,
X,
BB
)
2019-05-21 17:57:17.314700+0800 MGNSArray[704:68468] 删mtArr1 = (
)
2019-05-21 17:57:17.314767+0800 MGNSArray[704:68468] 删(
BB
)
2019-05-21 17:57:17.314795+0800 MGNSArray[704:68468] mtArr2 = (
AA,
BB
)
2019-05-21 17:57:17.314825+0800 MGNSArray[704:68468] mtArr2 = (
DD,
BB
)
2019-05-21 17:57:17.314854+0800 MGNSArray[704:68468] mtArr2 = (
BB,
DD
)
2019-05-21 17:57:17.314912+0800 MGNSArray[704:68468] mtArr2 = (
EEEE,
FF,
GG
)
2019-05-21 18:21:11.306863+0800 MGNSArray[738:74632] 排序前:(
10,
1,
3,
12,
22,
5,
33
)
2019-05-21 18:21:11.306919+0800 MGNSArray[738:74632] 冒泡排序后:(
1,
3,
5,
10,
12,
22,
33
)
/* NSArray.h
Copyright (c) 1994-2018, Apple Inc. All rights reserved.
*/
#import
#import
#import
#import
@class NSData, NSIndexSet, NSString, NSURL;
/**************** Immutable Array ****************/
NS_ASSUME_NONNULL_BEGIN
@interface NSArray<__covariant ObjectType> : NSObject
@property (readonly) NSUInteger count;
- (ObjectType)objectAtIndex:(NSUInteger)index;
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithObjects:(const ObjectType _Nonnull [_Nullable])objects count:(NSUInteger)cnt NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
@end
@interface NSArray (NSExtendedArray)
- (NSArray *)arrayByAddingObject:(ObjectType)anObject;
- (NSArray *)arrayByAddingObjectsFromArray:(NSArray *)otherArray;
- (NSString *)componentsJoinedByString:(NSString *)separator;
- (BOOL)containsObject:(ObjectType)anObject;
@property (readonly, copy) NSString *description;
- (NSString *)descriptionWithLocale:(nullable id)locale;
- (NSString *)descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level;
- (nullable ObjectType)firstObjectCommonWithArray:(NSArray *)otherArray;
- (void)getObjects:(ObjectType _Nonnull __unsafe_unretained [_Nonnull])objects range:(NSRange)range NS_SWIFT_UNAVAILABLE("Use 'subarrayWithRange()' instead");
- (NSUInteger)indexOfObject:(ObjectType)anObject;
- (NSUInteger)indexOfObject:(ObjectType)anObject inRange:(NSRange)range;
- (NSUInteger)indexOfObjectIdenticalTo:(ObjectType)anObject;
- (NSUInteger)indexOfObjectIdenticalTo:(ObjectType)anObject inRange:(NSRange)range;
- (BOOL)isEqualToArray:(NSArray *)otherArray;
@property (nullable, nonatomic, readonly) ObjectType firstObject API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
@property (nullable, nonatomic, readonly) ObjectType lastObject;
- (NSEnumerator *)objectEnumerator;
- (NSEnumerator *)reverseObjectEnumerator;
@property (readonly, copy) NSData *sortedArrayHint;
- (NSArray *)sortedArrayUsingFunction:(NSInteger (NS_NOESCAPE *)(ObjectType, ObjectType, void * _Nullable))comparator context:(nullable void *)context;
- (NSArray *)sortedArrayUsingFunction:(NSInteger (NS_NOESCAPE *)(ObjectType, ObjectType, void * _Nullable))comparator context:(nullable void *)context hint:(nullable NSData *)hint;
- (NSArray *)sortedArrayUsingSelector:(SEL)comparator;
- (NSArray *)subarrayWithRange:(NSRange)range;
/* Serializes this instance to the specified URL in the NSPropertyList format (using NSPropertyListXMLFormat_v1_0). For other formats use NSPropertyListSerialization directly. */
- (BOOL)writeToURL:(NSURL *)url error:(NSError **)error API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0));
- (void)makeObjectsPerformSelector:(SEL)aSelector NS_SWIFT_UNAVAILABLE("Use enumerateObjectsUsingBlock: or a for loop instead");
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(nullable id)argument NS_SWIFT_UNAVAILABLE("Use enumerateObjectsUsingBlock: or a for loop instead");
- (NSArray *)objectsAtIndexes:(NSIndexSet *)indexes;
- (ObjectType)objectAtIndexedSubscript:(NSUInteger)idx API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0));
- (void)enumerateObjectsUsingBlock:(void (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
- (void)enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
- (NSUInteger)indexOfObjectPassingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
- (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
- (NSUInteger)indexOfObjectAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
- (NSIndexSet *)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
- (NSIndexSet *)indexesOfObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
- (NSArray *)sortedArrayUsingComparator:(NSComparator NS_NOESCAPE)cmptr API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
- (NSArray *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator NS_NOESCAPE)cmptr API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
typedef NS_OPTIONS(NSUInteger, NSBinarySearchingOptions) {
NSBinarySearchingFirstEqual = (1UL << 8),
NSBinarySearchingLastEqual = (1UL << 9),
NSBinarySearchingInsertionIndex = (1UL << 10),
};
- (NSUInteger)indexOfObject:(ObjectType)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator NS_NOESCAPE)cmp API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)); // binary search
@end
@interface NSArray (NSArrayCreation)
+ (instancetype)array;
+ (instancetype)arrayWithObject:(ObjectType)anObject;
+ (instancetype)arrayWithObjects:(const ObjectType _Nonnull [_Nonnull])objects count:(NSUInteger)cnt;
+ (instancetype)arrayWithObjects:(ObjectType)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
+ (instancetype)arrayWithArray:(NSArray *)array;
- (instancetype)initWithObjects:(ObjectType)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
- (instancetype)initWithArray:(NSArray *)array;
- (instancetype)initWithArray:(NSArray *)array copyItems:(BOOL)flag;
/* Reads array stored in NSPropertyList format from the specified url. */
- (nullable NSArray *)initWithContentsOfURL:(NSURL *)url error:(NSError **)error API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0));
/* Reads array stored in NSPropertyList format from the specified url. */
+ (nullable NSArray *)arrayWithContentsOfURL:(NSURL *)url error:(NSError **)error API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0)) NS_SWIFT_UNAVAILABLE("Use initializer instead");
@end
@interface NSArray (NSDeprecated)
/* This method is unsafe because it could potentially cause buffer overruns. You should use -getObjects:range: instead.
*/
- (void)getObjects:(ObjectType _Nonnull __unsafe_unretained [_Nonnull])objects NS_SWIFT_UNAVAILABLE("Use 'as [AnyObject]' instead") API_DEPRECATED("Use -getObjects:range: instead", macos(10.0, 10.13), ios(2.0, 11.0), watchos(2.0, 4.0), tvos(9.0, 11.0));
/* These methods are deprecated, and will be marked with API_DEPRECATED in a subsequent release. Use the variants that use errors instead. */
+ (nullable NSArray *)arrayWithContentsOfFile:(NSString *)path;
+ (nullable NSArray *)arrayWithContentsOfURL:(NSURL *)url;
- (nullable NSArray *)initWithContentsOfFile:(NSString *)path;
- (nullable NSArray *)initWithContentsOfURL:(NSURL *)url;
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;
@end
/**************** Mutable Array ****************/
@interface NSMutableArray : NSArray
- (void)addObject:(ObjectType)anObject;
- (void)insertObject:(ObjectType)anObject atIndex:(NSUInteger)index;
- (void)removeLastObject;
- (void)removeObjectAtIndex:(NSUInteger)index;
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(ObjectType)anObject;
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithCapacity:(NSUInteger)numItems NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
@end
@interface NSMutableArray (NSExtendedMutableArray)
- (void)addObjectsFromArray:(NSArray *)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 API_DEPRECATED("Not supported", macos(10.0,10.6), ios(2.0,4.0), watchos(2.0,2.0), tvos(9.0,9.0));
- (void)removeObjectsInArray:(NSArray *)otherArray;
- (void)removeObjectsInRange:(NSRange)range;
- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray range:(NSRange)otherRange;
- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray;
- (void)setArray:(NSArray *)otherArray;
- (void)sortUsingFunction:(NSInteger (NS_NOESCAPE *)(ObjectType, ObjectType, void * _Nullable))compare context:(nullable void *)context;
- (void)sortUsingSelector:(SEL)comparator;
- (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes;
- (void)removeObjectsAtIndexes:(NSIndexSet *)indexes;
- (void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray *)objects;
- (void)setObject:(ObjectType)obj atIndexedSubscript:(NSUInteger)idx API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0));
- (void)sortUsingComparator:(NSComparator NS_NOESCAPE)cmptr API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
- (void)sortWithOptions:(NSSortOptions)opts usingComparator:(NSComparator NS_NOESCAPE)cmptr API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
@end
@interface NSMutableArray (NSMutableArrayCreation)
+ (instancetype)arrayWithCapacity:(NSUInteger)numItems;
+ (nullable NSMutableArray *)arrayWithContentsOfFile:(NSString *)path;
+ (nullable NSMutableArray *)arrayWithContentsOfURL:(NSURL *)url;
- (nullable NSMutableArray *)initWithContentsOfFile:(NSString *)path;
- (nullable NSMutableArray *)initWithContentsOfURL:(NSURL *)url;
@end
NS_ASSUME_NONNULL_END