美文网首页
object-c 之NSArray

object-c 之NSArray

作者: 墨凌风起 | 来源:发表于2016-12-29 09:28 被阅读60次
//在c语言中,数组中包含元素的类型是相同的,oc中数组,可包含可不同类型

      NSArray * array =@[@"hello",
                           [NSNumber numberWithInteger:10],
                           [NSDate date]];
        
        NSLog(@"array = %@",array);
    /*******************构造方法创建*******************/
       // 1 创建空数组
        NSArray *array1 = [[NSArray alloc]init];

       // 2通过传入元素 构造数组
        NSArray *array2 = [[NSArray alloc]initWithObjects:@"hello",@"nihao",@"world", nil];
        NSLog(@"array2 = %@",array2);

        //3 传入数组 构造新数组
        NSArray *array3 = [[NSArray alloc]initWithArray:array2];
        NSLog(@"array3 = %@",array3);
/*********************类方法**************************/
    //1 创建空数组   //用于新建数组的初始化
    NSArray *array1  = [NSArray array];

    //2传入一个元素创建数组
    NSArray *array2 = [NSArray arrayWithObject:@"hello"];
    NSLog(@"array2 = %@",array2);
    
    //3 传入元素创建数组
    NSArray *array3 = [NSArray arrayWithObjects:@"a",@"b",@"c", nil];
    NSLog(@"array3 = %@",array3);
    
    //4 通过传入数组创建数组
    NSArray *array4 = [NSArray arrayWithArray:array3];
    NSLog(@"array4 = %@",array4);

    //5.使用一个数组中的某个范围 构造一个新数组
    NSArray *subArray = [array subarrayWithRange:NSMakeRange(0, 3)];
    NSLog(@"subArray = %@",subArray);
    
    //6 用字符串指针类型数组构造数组
    NSString *strings[3];//数组中有3个字符串类型的元素
    strings[0] = @"one";
    strings[1] = @"two";
    strings[2] = @"three";
    
    NSArray *stringArray = [NSArray arrayWithObjects:strings count:3];
    NSLog(@"stringArray = %@",stringArray);
    
    //数组的元素个数
    NSArray *array = @[@"one",@"two",@"three",@"four",@"five"];
    NSUInteger count = [array count];
    NSLog(@"数组元素array的元素个数为:cnt = %lu",count);
    
    // 获取数组下标元素
    id obj = [array objectAtIndex:0];
   // 判断是否属于某个类 对象 [isKindOfClass:[类 class]]
    if ([obj isKindOfClass:[NSString class]])
    {
        NSLog(@"obj = %@",obj);
    }
    
    //添加元素
    NSArray *array1 = [array arrayByAddingObject:@"six"];
    NSLog(@"array1 = %@",array1);
    
    //添加一个数组的所有元素 到另一个数组
    NSArray *array2 = [array arrayByAddingObjectsFromArray:array1];
    NSLog(@"array2 = %@",array2);
    
    //把数组元素 通过特定拼接符 连接在一起
    NSString *ArrayString = [array1 componentsJoinedByString:@" "];
    NSLog(@"arrayString = %@",ArrayString);
    
    //把字符串通过某个分隔符切割成数组
    NSArray *StringArray = [ArrayString componentsSeparatedByString:@" "];
    NSLog(@"stringArray = %@",StringArray);
    
    // 判断数组中是否含有某个元素
    BOOL ret = [array containsObject:@"one"];
    NSLog(@"ret = %d",ret);
    
    // 返回数组在传入数组中最先出现的元素
    id obj1 = [array firstObjectCommonWithArray:array1];
    NSLog(@"obj1 = %@",obj1);
    
    //返回元素在数组中第一次出现的下标
    NSUInteger index =  [array indexOfObject:@"one"];
    NSLog(@"index = %lu",index);
    
    //返回元素在数组中某个范围内第一次出现的下标
    NSUInteger index1 = [array indexOfObject:@"one" inRange:NSMakeRange(0, 3)];
    NSLog(@"index1 = %lu",index1);
    
    //判断两个数组是否相同
    BOOL ret2 =  [array isEqualToArray:array1];
    NSLog(@"ret = %d",ret2);
    
    //获取数组第一个元素
    id obj2 = [array firstObject];
    NSLog(@"obj2 = %@",obj2);
    
    //获取数组最后一个元素
    id obj3 = [array lastObject];
    NSLog(@"obj3 = %@",obj3);
    
    //数组的遍历
    //1 通过数组的个数进行遍历
    for (NSInteger i =0; i<[array count]; i++)
    {
        NSLog(@"%li i = %@",i,array[i]);
    }
    //2 for-in遍历
     for (id obj in array)
     {
        NSLog(@"%@",obj);
     }
    //3 枚举器遍历  正序
    NSEnumerator * enumerator = [array objectEnumerator];
    id obj4 = nil;
    while (obj4 = [enumerator nextObject])
    {
        NSLog(@"obj4= %@",obj4);
    }
    NSLog(@"-------------");
    //逆序
    NSEnumerator * reverseEnumerator = [array reverseObjectEnumerator];
    id obj5 = nil;
    while (obj5 = [reverseEnumerator nextObject])
    {
        NSLog(@"obj5 = %@",obj5);
    }
    //提取下标集合内的元素
    
    NSMutableIndexSet * indexSet = [NSMutableIndexSet indexSetWithIndex:1];
    
    [indexSet addIndex:2];
    [indexSet addIndex:3];
    
    NSArray *indexArray = [array objectsAtIndexes:indexSet];
    NSLog(@"indexArray = %@",indexArray);

相关文章

网友评论

      本文标题:object-c 之NSArray

      本文链接:https://www.haomeiwen.com/subject/yfvuvttx.html