iOS中的NSArray和NSMutableArray

2023-11-22 08:40:44 浏览数 (3)

NSArray基本用法

代码语言:javascript复制
    //初始化
    NSArray *array = [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F", nil];
    NSArray *array1 = @[@"a",@"b",@"c",@"d"];
      
    //获取数组元素个数
    NSLog(@"%lu",(unsigned long)array.count);//6
    //获取数组最后一个元素
    NSLog(@"%@",[array lastObject]);//F
    //获取数组第一个元素
    NSLog(@"%@",[array firstObject]);//A
    //获取对象C在数组的位置
    NSLog(@"%lu",(unsigned long)[array indexOfObject:@"C"]);//  2
     //获取数组中索引为2的对象
    NSLog(@"%@",[array objectAtIndex:2]);//C

NSArray遍历

代码语言:javascript复制
    for (int i = 0 ; i< array.count; i  ) {
        NSLog(@"遍历-%@",array[i]);
    }
    
    
    for (NSString *str in array) {
        NSLog(@"遍历-%@",str);
    }

    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"遍历-%@-%lu",obj,(unsigned long)idx);
    }];

NSArray中的所有对象执行方法

代码语言:javascript复制
@interface Student : NSObject
- (void)log;
- (void)logInfo:(NSString *)str;
@end

@implementation Student
- (void)log{
    NSLog(@"打印");
}
- (void)logInfo:(NSString *)str{
    NSLog(@"打印-%@",str);
}
@end

---------------------
NSArray *array3 = @[[Student new],[Student new],[Student new],[Student new]];
[array3 makeObjectsPerformSelector:@selector(log)];
[array3 makeObjectsPerformSelector:@selector(logInfo:) withObject:@"p"];

NSArray排序

代码语言:javascript复制
    NSArray *arr = @[@"90",@"20",@"30",@"10",@"50",];
    //数组中的元素不可以是自定义的
    NSArray *arr1 = [arr sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"%@",arr1);
    /*
     10,20,30,50,90
     */

    //NSSortStable:串行排序   NSSortConcurrent:并发排序
    NSArray *arr2 = [arr sortedArrayWithOptions:NSSortStable usingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
        //每次调用
        NSLog(@"ob1--%@,obj2--%@",obj1,obj2);
        return [obj1 intValue] < [obj2 intValue];
    }];
    NSLog(@"%@",arr2);
     /*
     90,50,30,20,10
     */

NSArray和NSString转换

代码语言:javascript复制
    NSArray *arr = @[@"A",@"B",@"C",@"D"];
    NSString *str = [arr componentsJoinedByString:@"-"];
    NSLog(@"%@",str);//A-B-C-D

    NSString *str1 = @"A-B-C-D";
    NSArray *arr1 = [str1 componentsSeparatedByString:@"-"];
    NSLog(@"%@",arr1);
    /*
     A,B,C,D
     */

NSArray文件读写

代码语言:javascript复制
    NSArray *array = [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F", nil];
    //将数组写入文件,本质是写入一个XML文件,iOS中一般写入plist,不能写入自定义对象
    BOOL flag = [array writeToFile:@"/Users/soso/Desktop/demo1/NSArray/demo.plist" atomically:YES];
    NSLog(@"%d",flag);

    //读文件
    NSArray *arr = [NSArray arrayWithContentsOfFile:@"/Users/soso/Desktop/demo1/NSArray/demo.plist"];
    NSLog(@"%@",arr);//A,B,C,D,E,F

NSMutableArray

代码语言:javascript复制
   NSMutableArray *arr = [NSMutableArray array];
    //添加元素
    [arr addObject:@"A"];
    NSLog(@"%@",arr);//A
    
    //将指定数组中的元素都取出来,放到arrM中
    [arr addObjectsFromArray:@[@"B",@"C"]];
    NSLog(@"%@",arr);// A,B,C

    //插入一个元素
    [arr insertObject:@"D" atIndex:2];
    NSLog(@"%@",arr);// A,B,D,C

    //插入多个元素
    NSRange range = NSMakeRange(2, 2);
    NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:range];
    [arr insertObjects:@[@"H",@"G"] atIndexes:set];
    NSLog(@"%@",arr);//A,B,H,G,D,C

    //删除
    [arr removeObject:@"B"];
    [arr removeObjectAtIndex:2];
    [arr removeLastObject];
    [arr removeAllObjects];
    
    //替换
    [arr replaceObjectAtIndex:3 withObject:@"L"];
    NSLog(@"%@",arr);//A,B,H,L,D,C

0 人点赞