NSArray,数组,用于管理对象的有序集合。
NSArray的用法总结如下:
1.数组的工厂方法创建:
#pragma mark 创建(+)
-(void)Creating
{
// 空数组
NSArray *array = [NSArray array];
// 根据数组创建数组
array = [NSArray arrayWithArray:array];
// 从文件获取
NSString *filePath = [self testData];
array = [NSArray arrayWithContentsOfFile:filePath];
array = [NSArray arrayWithContentsOfURL:[NSURL fileURLWithPath:filePath]];
// 包含一个数据的数组
array = [NSArray arrayWithObject:@"Hello"];
// 包含多个数据的数组
array = [NSArray arrayWithObjects:@"Hello", @"World", nil];
}
2.数组初始化创建:
#pragma mark 初始化(-)
-(void)Initializing
{
// 空数组
NSArray *array = [[NSArray alloc]init];
// 根据数组创建数组
array = [[NSArray alloc] initWithArray:array];
array = [[NSArray alloc] initWithArray:array copyItems:YES];
// 从文件获取
NSString *filePath = [self testData];
array = [[NSArray alloc] initWithContentsOfFile:filePath];
array = [[NSArray alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath]];
// 包含多个数据的数组
array = [[NSArray alloc] initWithObjects:@"Hello",@"World", nil];
}
3.数组查询
#pragma mark 查询
-(void)Querying
{
NSArray<NSString *> *array = [NSArray arrayWithObjects:@"Hello", @"World", nil];
//数组中是否包含某个数据
BOOL isContains = [array containsObject:@"Hello"];
NSLog(@"containsObject:%d",isContains);
// 数组长度
NSInteger count = [array count]; // 数组长度
NSLog(@"count:%ld", (long)count);
//第一个数据
id str = array.firstObject;
//最后一个数据
str = array.lastObject;
// 取第0个位置的数据
str = [array objectAtIndex:0];
// 取第0个位置的数据
str = [array objectAtIndexedSubscript:0];
// 取第0个位置的数据
str = array[0];
// 遍历
NSEnumerator *enumerator = [array objectEnumerator];
id anObject;
while (anObject = [enumerator nextObject]) {
NSLog(@"objectEnumerator:%@", anObject);
}
enumerator = [array reverseObjectEnumerator];
while (anObject = [enumerator nextObject]) {
NSLog(@"reverseObjectEnumerator:%@", anObject);
}
// 快速遍历
for (anObject in array) {
NSLog(@"forIn:%@", anObject);
}
}
4.数组查找对象位置
#pragma mark 查找对象位置
-(void)FindingObjects
{
NSArray<NSString *> *array = [NSArray arrayWithObjects:@"Hello", @"World",@"Hello",@"IOS", nil];
// 查找数据存在的位置
NSInteger index = [array indexOfObject:@"Hello"];
// 从指定的范围查找对象
NSRange range = {0, array.count};
index = [array indexOfObject:@"IOS" inRange:range];
index = [array indexOfObjectIdenticalTo:@"World"];
index = [array indexOfObjectIdenticalTo:@"World" inRange:range];
// 自定义查找
// 查找单个
index = [array indexOfObjectPassingTest:^BOOL(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// *stop 是否停止
// rutern,是否找到
if ([@"World" isEqualToString:obj]) {
*stop = YES;
return YES;
}
return NO;
}];
// 多核查找单个
index = [array indexOfObjectWithOptions:NSEnumerationConcurrent passingTest:^BOOL(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// *stop 是否停止
// rutern,是否找到
if ([@"World" isEqualToString:obj]) {
*stop = YES;
return YES;
}
return NO;
}];
// 查找多个
NSIndexSet *set = [array indexesOfObjectsPassingTest:^BOOL(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([@"Hello" isEqualToString:obj] || [@"IOS" isEqualToString:obj]) {
return YES;
}
return NO;
}];
// 多核查找多个
set = [array indexesOfObjectsWithOptions:NSEnumerationConcurrent passingTest:^BOOL(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([@"Hello" isEqualToString:obj] || [@"IOS" isEqualToString:obj]) {
return YES;
}
return NO;
}];
}
5.给数组每个元素发送消息
#pragma mark 每个元素发送消息
-(void)SendingMessageToElements
{
NSArray *tArray = [NSArray array];
NSArray *array = [NSArray arrayWithObjects:tArray, tArray, nil];
// 通知数组中的每个元素执行方法
[array makeObjectsPerformSelector:@selector(count)];
// 携带参数发出通知
[array makeObjectsPerformSelector:@selector(containsObject:) withObject:@"Hello"];
// 自定义发出通知
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"enumerateObjectsUsingBlock:%lu", (unsigned long)idx);
}];
// 多核自定义通知
[array enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"enumerateObjectsWithOptions:%lu", (unsigned long)idx);
}];
// 根据索引发出通知
NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:0];
[array enumerateObjectsAtIndexes:indexSet options:NSEnumerationConcurrent usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"enumerateObjectsAtIndexes:%lu", (unsigned long)idx);
}];
}
6.数组比较
#pragma mark 数组比较
-(void)Comparing
{
NSArray<NSString *> *array = [NSArray arrayWithObjects:@"Hello", @"World", @"IOS", nil];
NSArray<NSString *> *array2 = [NSArray arrayWithObjects:@"Hello", @"World", nil];
// 返回第一个相同的数据
NSString *str = [array firstObjectCommonWithArray:array2];
NSLog(@"firstObjectCommonWithArray:%@", str);
// 数组内的内容是否相同
BOOL isEqual = [array isEqualToArray:array2];
NSLog(@"isEqual:%d", isEqual);
}
7.生成新数组
#pragma mark 生成新数组
-(void)DerivingNewArray
{
NSArray<NSString *> *array = [NSArray arrayWithObjects:@"Hello", @"World", nil];
// 添加单个数据,并生成一个新的数组
array = [array arrayByAddingObject:@"IOS"];
// 添加多个数据,并返回一个新的数组
array = [array arrayByAddingObjectsFromArray:array];
// 通过过滤器筛选数组
NSPredicate *predicate = [NSPredicate predicateWithValue:YES];
array = [array filteredArrayUsingPredicate:predicate];
// 通过范围生成数组
NSRange range = {0, 2};
array = [array subarrayWithRange:range];
}
8.数组排序
/** sortedArrayUsingFunction排序*/
NSInteger sortByFunction(NSString * obj1, NSString * obj2, void * context)
{
return [obj1 compare:obj2];
}
-(void)Sorting
{
NSArray<NSString *> *array = [NSArray arrayWithObjects:@"Hello", @"World", @"IOS", nil];
// Function 排序
array = [array sortedArrayUsingFunction:sortByFunction context:nil];
NSData *sortedArrayHint = array.sortedArrayHint;
array = [array sortedArrayUsingFunction:sortByFunction context:nil hint:sortedArrayHint];
// Selector 排序
array = [array sortedArrayUsingSelector:@selector(compare:)];
// Block排序
array = [array sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj1 compare:obj2];
}];
// 并发block排序
array = [array sortedArrayWithOptions:NSSortConcurrent usingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj1 compare:obj2];
}];
}
9.处理字符串数组
#pragma mark 处理字符串数组
-(void)WorkingWithStringElements
{
NSArray<NSString *> *array = [NSArray arrayWithObjects:@"Hello", @"World", @"IOS", nil];
// 数组中的NSString元素拼接
NSString *str = [array componentsJoinedByString:@","];
NSLog(@"componentsJoinedByString:%@", str);
}
10.存储
#pragma mark 存储
- (void)CreatingDescription {
NSArray<NSString *> *array = [NSArray arrayWithObjects:@"Hello", @"World", @"IOS", nil];
// 描述信息
NSString *description = array.description;
description = [array descriptionWithLocale:nil];
description = [array descriptionWithLocale:nil indent:1];
// 获取应用中Document文件夹
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// 存储的路径
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"test.plist"];
// 写入
BOOL write = [array writeToFile:filePath atomically:YES];
write = [array writeToURL:[NSURL fileURLWithPath:filePath] atomically:YES];
}
11.可变数组初始化
#pragma mark 测试数据
- (NSString *)testData {
// 获取应用中Document文件夹
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// 测试数据
NSArray *array = [NSArray arrayWithObjects:@"Hello", @"World", nil];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"test.plist"];
BOOL write = [array writeToFile:filePath atomically:YES]; // 输入写入
NSLog(@"writeToFile:%d", write);
return filePath;
}
#pragma mark 初始化
- (void)CreatingAndInitializing {
NSString *filePath = [self testData];
// (+)创建
NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:1];
// 根据文件路径创建数组
mArray = [NSMutableArray arrayWithContentsOfFile:filePath];
mArray = [NSMutableArray arrayWithContentsOfURL:[NSURL fileURLWithPath:filePath]];
// (-)创建
mArray = [[NSMutableArray alloc] initWithCapacity:1];
mArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
mArray = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath]];
}
12.可变数组增加元素
#pragma mark 增加数据
- (void)Adding {
NSMutableArray *mArray = [NSMutableArray array];
// 增加单一数据
[mArray addObject:@"iOS"];
// 批量添加数据
NSArray *array = [NSArray arrayWithObjects:@"1", @"2", nil];
[mArray addObjectsFromArray:array];
// 指定位置插入单一数据
[mArray insertObject:@"IOS" atIndex:1];
NSLog(@"%@",mArray);
// 指定位置插入多个数据
NSRange range = {1, array.count};
NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndexesInRange:range];
[mArray insertObjects:array atIndexes:indexSet];
NSLog(@"%@",mArray);
}
13.可变数组删除元素
#pragma mark 删除数据
- (void)Removing {
NSMutableArray *mArray = [NSMutableArray arrayWithObjects:@"Hello", @"World", @"IOS", nil];
// 删除所有元素
[mArray removeAllObjects];
// 删除最后一个元素
mArray = [NSMutableArray arrayWithObjects:@"Hello", @"World", @"IOS", nil];
[mArray removeLastObject];
// 根据位置删除对象
mArray = [NSMutableArray arrayWithObjects:@"Hello", @"World", @"IOS", nil];
[mArray removeObjectAtIndex:0];
// 根据数组删除对象
mArray = [NSMutableArray arrayWithObjects:@"Hello", @"World", @"IOS", @"IOS", nil];
NSArray *array = [NSArray arrayWithObjects:@"IOS",@"Hello", nil];
[mArray removeObjectsInArray:array];
// 根据对象删除
mArray = [NSMutableArray arrayWithObjects:@"Hello", @"IOS", @"World", @"IOS", nil];
[mArray removeObject:@"IOS"];
// 或
mArray = [NSMutableArray arrayWithObjects:@"Hello", @"IOS", @"World", @"IOS", nil];
[mArray removeObjectIdenticalTo:@"IOS"];
// 删除指定范围内的对象
NSRange range = {0, 2};// 第0个位置开始,连续2个
mArray = [NSMutableArray arrayWithObjects:@"Hello", @"IOS", @"World", @"IOS", nil];
[mArray removeObject:@"IOS" inRange:range];
// 或
mArray = [NSMutableArray arrayWithObjects:@"Hello", @"IOS", @"World", @"IOS", nil];
[mArray removeObjectIdenticalTo:@"IOS" inRange:range];
// 删除指定NSRange范围内的对象,批量删除
mArray = [NSMutableArray arrayWithObjects:@"Hello", @"World", @"IOS", nil];
[mArray removeObjectsInRange:range];
// 或
mArray = [NSMutableArray arrayWithObjects:@"Hello", @"World", @"IOS", nil];
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
[mArray removeObjectsAtIndexes:indexSet];
}
14.可变数组替换元素
#pragma mark 替换对象
- (void)Replacing{
NSMutableArray *mArray = [NSMutableArray arrayWithObjects:@"Hello", @"World", @"IOS", nil];
NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
NSRange range = {0, array.count};// 第0个位置开始,连续count个
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
// 指定位置替换对象
[mArray replaceObjectAtIndex:0 withObject:@"xixi"];
// or
[mArray setObject:@"xixi" atIndexedSubscript:0];
// 数组替换
[mArray setArray:array];
// 用array替换数组中指定位置的所有元素
mArray = [NSMutableArray arrayWithObjects:@"Hello", @"World", @"IOS", nil];
[mArray replaceObjectsInRange:range withObjectsFromArray:array];
// or
mArray = [NSMutableArray arrayWithObjects:@"Hello", @"World", @"IOS", nil];
[mArray replaceObjectsAtIndexes:indexSet withObjects:array];
// 局部替换,使用array中的部分元素替换目标数组指定位置的元素
mArray = [NSMutableArray arrayWithObjects:@"Hello", @"World", @"IOS", nil];
range.length = 2;
[mArray replaceObjectsInRange:range withObjectsFromArray:array range:range];
}
15.可变数组过滤
#pragma mark 数组过滤
- (void)Filtering {
NSMutableArray *mArray = [NSMutableArray arrayWithObjects:@"Hello", @"World", @"IOS", nil];
// 使用过滤器过滤数组中的元素
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
NSLog(@"%@", evaluatedObject);
if ([@"Hello" isEqualToString:evaluatedObject]) {
return YES;
}
return NO;
}];
[mArray filterUsingPredicate:predicate];
}
16.可变数组排序
/** sortedArrayUsingFunction排序*/
NSInteger mSortByFunction(NSString * obj1, NSString * obj2, void * context) {
return [obj1 compare:obj2];
}
#pragma mark 排序
- (void)Sorting {
NSMutableArray *mArray = [NSMutableArray arrayWithObjects:@"Hello", @"World", @"IOS", nil];
// 交换两个位置的数据
[mArray exchangeObjectAtIndex:0 withObjectAtIndex:1];
// 对象自带的方法排序
[mArray sortUsingSelector:@selector(compare:)];
// Function排序
[mArray sortUsingFunction:mSortByFunction context:nil];
// block排序
[mArray sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj1 compare:obj2];
}];
[mArray sortWithOptions:NSSortConcurrent usingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj1 compare:obj2];
}];
}
demo地址:demo