2、iOS_NSArray(数组)

数组

一、数组
1、C语言数组与OC数组区别
C语言数组:具有相同类型的元素的有序集合
int a[10]
int *b[2]
OC中:任意类型对象的指针的集合
数组中的元素都是指针变量,这些指针变量指向某个对象

2、不可变数组NSArray:

(1)创建数组
//创建数组,以nil结尾,数组仅包括nil之前的元素
        //读到nil,编译器就认为数组结束
        NSArray *arr = [[NSArray alloc] initWithObjects:@"hello",@"world", nil];
        NSLog(@"%@", arr);
       
        //通过一个数组创建一个新的数组
        NSArray *arr1 = [[NSArray alloc] initWithArray:arr];
        NSLog(@"%@", arr1);

        //通过类方法创建数组
        NSArray *arr2 = [NSArray arrayWithObjects:@"guanxi", @"baizhi", @"ajiao", nil];
        NSLog(@"%@", arr2);
       
        NSArray *arr3 = [NSArray arrayWithArray:arr2];
        NSLog(@"%@", arr3);

(2)数组元素个数
NSUInteger count = [arr3 count];

(3)通过指定索引获取元素
NSString *str = [arr3 objectAtIndex:0];

(4)获取指定对象的索引
NSUInteger index = [arr3 indexOfObject:@"ajiao"];

(5)判断是否包含指定对象
BOOL ret = [arr3 containsObject:@"tingfeng"];

(6)遍历数组元素
for (int i = 0; i < [arr3 count]; i++) {
            NSLog(@"%@", [arr3 objectAtIndex:i]);
        }
        //快速枚举
        for (NSString *str in arr3) {
            NSLog(@"%@", str);
        }
}

[arr3 fitstObject];数组第一位
[arr3 lastObject];数组最后一位


3、数组中存放不同类型对象指针
(1)description
//如果实现了description方法,当通过%@打印对象内容的时候,就会输出相关数据
//如果没有该方法,就会打印对象地址

(2)isKindOfClass
isKindOfClass判断指针指向的对象是否是某个类的实例,或者是否是某个类的子类的实例
if ([obj isKindOfClass:[Dog class]]) {
                [obj bark];
            }else{
                NSLog(@"%@", obj);
            }

//Class类型 存放类的信息
            //[类名/对象的指针  class]
            /*
            //通过字符串获取Class
            Class cls = NSClassFromString(@"Dog");
            //通过Class类型的变量可以创建对象
            Dog *dog = [[cls alloc] init];
            [dog bark];
            //通过Class类型变量获取类名
            NSString *str = NSStringFromClass([wangcai class]);
            NSLog(@"%@", str);

4、字符串分割
//使用指定的字符串分割另一个字符串
        NSArray *arr = [str componentsSeparatedByString:@" "];

//NSCharacterSet 按照字符集中字符分割字符串
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@" ,"];
        NSArray *arr1 = [str1 componentsSeparatedByCharactersInSet:set];

 //找到分割条件
    for (NSInteger i = 0; i < fileContent.length; i++) {
        unichar c = [fileContent characterAtIndex:i];
  //判断是否是字母
        if (!isalpha(c)) {
            str = [str stringByAppendingFormat:@"%C",c];
        }
    }
    NSArray *arr = [fileContent componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:str]];//按照字符集合分割


5、字符串拼接
//按照指定字符串拼接字符串数组中元素
        NSString *str = [arr componentsJoinedByString:@"###"];

6、可变数组 NSMutableArray
        NSMutableArray *mArr = [[NSMutableArray alloc] initWithObjects:@"123", @"tom", nil];
        //在数组最后添加元素
        [mArr addObject:@"jerry"];
       
        //在指定索引处插入元素
        [mArr insertObject:@"hello" atIndex:1];
       
        //删除指定元素
        [mArr removeObject:@"hello"];

        //删除指定索引处的元素
        [mArr removeObjectAtIndex:1];

        //删除所有元素
//        [mArr removeAllObjects];
//        NSLog(@"%@", mArr);
       
        //替换指定索引处的元素
        [mArr replaceObjectAtIndex:1 withObject:@"world"];

        //交换指定索引处的元素
        [mArr exchangeObjectAtIndex:0 withObjectAtIndex:1];

        //可变转化为不可变
        [arr copy];

二、数组
1、不可变数组
#创建数组#
//创建方式1
NSArray *array1 = [[NSArray alloc] initWithObjects:@"xixi",@"haha",@"hehe", nil];
//创建方式2
NSArray *array2 = [[NSArray alloc] initWithArray:array1];
//创建方式3
NSArray *array3 = @[@"xixi",@"haha",@"hehe"];
//创建方式4
NSArray *array4 = [NSArray arrayWithObjects:@"how",@"are",@"you", nil];
//创建方式5
NSArray *array5 = [NSArray arrayWithArray:array4];

#查找元素位置#
NSArray *array6 = @[@"how", @"are", @"you"];
NSUInteger index = [array6 indexOfObject:@"are"];
//结果:index = 1

#字符串分割到数组里#
NSString *str = @"You@are@the@best";
NSArray *array7 = [str componentsSeparatedByString:@"@"];
//结果:array7 = @[@"You",@"are",@"the",@"best"];

#数组连接成字符串#
NSArray *array8 = @[@"I", @"love", @"you"];
NSString *str = [array8 componentsJoinedByString:@"+"];
//结果:str = @"I+love+you"
2、可变数组
#创建数组#
//创建方式1
NSMutableArray * mArray1 = [[NSMutableArray alloc] initWithObjects:@"xixi",@"haha",@"hehe", nil];
//创建方式2
NSMutableArray * mArray2 = [[NSMutableArray alloc] initWithArray:mar1];
//创建方式3
NSMutableArray * mArray3 = [NSMutableArray arrayWithObjects:@"how",@"are",@"you", nil];
//创建方式4
NSMutableArray * mArray4 = [NSMutableArray arrayWithArray:mar3];

#增删替换#
NSArray * array = @[@"how",@"do",@"you"];
NSMutableArray * mArray5 = [[NSMutableArray alloc] initWithArray:array];

//添加
[mArray5 addObject:@"do"];
//结果:mArray5 = @[@"how", @"do", @"you", @"do"]

//插入
[mArray5 insertObject:@"GG" atIndex:0];
//结果:mArray5 = @[@"GG", @"how", @"do", @"you", @"do"]

//删除
[mArray5 removeObjectAtIndex:0];
//结果:mArray5 = @[@"how", @"do", @"you", @"do"]

[mArray5 removeLastObject];
//结果:mArray5 = @[@"how", @"do", @"you"]

//替换
[mArray5 replaceObjectAtIndex:0 withObject:@"what"];
//结果:mArray5 = @[@"what", @"do", @"you"]

//交换位置
[mArray5 exchangeObjectAtIndex:0 withObjectAtIndex:1]
//结果:mArray5 = @[@"do", @"what", @"you"]

你可能感兴趣的:(2、iOS_NSArray(数组))