iOS数组,字典,集合

数组

  • 1、固定数组
    1.创建数组(不可变数组创建后不可变,在创建的时候要初始化)
        //对象方法 [[NSArray alloc] initWithObjects:(id),nil]
        NSArray * array = [[NSArray alloc] initWithObjects:@"one",@"2",@"2.3",@"a",nil];    //array == (one,2,"2.3",a);
        //类方法 [NSArray arrayWithObjects:(id),nil]
        NSArray * array1 = [NSArray arrayWithObjects:@"one",@"2",@"2.3",@"a",nil];  //array == (one,2,"2.3",a);

        //次方法最后一个元素必须是nil,如果数组中间存在 nil 元素,容易导致数据丢失
        NSString * str = nil;
        NSArray * array4 = [[NSArray alloc] initWithObjects:@"oe",@"2",str,@"t3",@"3.14", nil]; //array4 = (oe,2);

        //快速方法 @[(id)]
        NSArray * array3 = @[@"one#",@"23",@"2.3",@"a"];    //array = ("one#",23,"2.3
    2.基本类型转换成数组对象

        //数组中可以存储不同类型的对象
        int i = 10;
        float f = 3.14;
        NSNumber * number = [NSNumber numberWithInt:i];
        NSNumber * number1 = [NSNumber numberWithFloat:f];
        NSArray * array = @[@"one",@"10",number,number1];   // array == (one,10,10,"3.14");

        //数组中存储的是对象的地址,数组中也可以存储数组的地址
        NSArray * array1 = @[@"1",@"2",@"3"];
        NSArray * array2 = @[array,array1];

    3.数组中自定义对象
        //创建三个对象(类以及创建)
        Person * p1 = [[Person alloc] initWithName:@"Jack" andAge:15];
        Person * p2 = [[Person alloc] initWithName:@"Tom" andAge:20];
        Person * p3 = [[Person alloc] initWithName:@"Lucy" andAge:18];
        NSArray array4 = @[p1,p2,p3];

    4.获取数组中的元素
        Person * p = [array4 objectAtIndex:0];  //name = Jack,age = 15
        Person * p = array4[0]; //快速方法

    5.数组中元素个数
        NSUInteger count = [array4 count];
        NSLog(@"%lu",count);

    6.判断数组中是否包含某个元素
        Person * p4 = [[Person alloc] initWithName:@"Tom" andAge:20];
        NSLog(@"p2=%p,p4=%p",p2,p4);    //p2和p4的地址不相同,p4为新对象
        if ([array4 containsObject:p4]) {
            NSLog(@"包含");
        } else {
            NSLog(@"不包含");
        }
        //不包含,p4不在array4中,即使内容相同,但是也不包含

        NSArray * array5 = @[@"one",@"two",@"three"];
        NSString * str2 = @"one";
        NSString * str4 = @"one";
        NSString * str3 = [[NSString alloc] initWithFormat:@"%@",@"two"];
        NSLog(@"%p %p %p %p",str2,str3,str4,array9[0]);  //0x100002088 0x6f777435 0x100002088
        //在创建字符串的时候会判断内存是否含有相同字符串,若有相同的则不会新开辟内存去存储,只把新的字符串指针指向那个地址,若没有相同的则就会新开辟空间去存储
        //但是 str3 是重新初始化 NSString,所以str3的地址是固定的 并不和array[1]的地址一样
        if ([array9 containsObject:str3]) {
            NSLog(@"包含");
        } else {
            NSLog(@"不包含");
        }
        //包含

    7.遍历数组
        NSArray * array = @[@"1",@"2",@"three",@"4"];
        //方法一
        for (int i=0; i<[array count]; i++) {
            NSLog(@"array[%d]=%@",i,array[i]);
        }
        //方法二
        for (id * str in array) {
            NSLog(@"%@",str);
        }

    8.数组排序(排序后放在一个新的NSArray数组中)
        NSArray * array = @[@"a",@"b",@"f",@"d",@"c"];

        //传入一个比较大小的方法 根据返回值来决定是否需要交换元素
        NSArray * array2 = [array sortedArrayUsingSelect:sel];
        SEL sel = @selector(compare:);  //a-b-c-d-f
        SEL sel2 = @selector(isGreatThan:); //a-b-c-d-f
        SEL sel3 = @selector(isLessThan:);  //f-d-c-b-a

        //block
        //返回值 (^名字)(参数列表)
        NSArray * array3 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            return [obj1 compare:obj2];
        }];     //a-b-c-d-f
        NSArray * array4 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            return [obj1 compare:obj2];
        }];     //f-d-c-b-a
  • 2、可变数组
    1.创建可变数组
        NSArray * array = @[@"one",@"two",@"three",@"four"];
        NSMutableArray * muArr = [[NSMutableArray alloc] initWithArray:array];
        NSMutableArray * muArr2 = [NSMutableArray arrayWithObjects:@"one",@"two",@"three",nil];

    2.添加元素
        NSMutableArray * muArr3 = [[NSMutableArray alloc] init];
        [muArr3 addObject:@"one"];

    3.添加其他数组的元素
        [muArr3 addObjectsFromArray:array];

    4.在指定位置插入元素
        [muArr3 insetObject:@"a" atIndex:1];

    5.删除元素  会通过对象地址删除数组中所有的用同一个地址的对象
        [muArr removeObject:@"one"];    //删除数组中的所有指向"one"地址的元素

    6.通过索引方式删除对象(索引值不能数组越界)
        [muArr removeObjectAtIndex:0];

    7.删除所有元素
        [muArr removeAllObjects];

    8.交换数组元素    //- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;
         [muArr exchangeObjectAtIndex:i withObjectAtIndex:j]
  • 3、数组转换
    1.不可变数组到可变数组
        NSArray * array = @[@"one",@"two"];
        NSMutableArray * muArr = [[NSMutableArray alloc] initWithArray:array];  // [array mutableCopy];

    2.可变数组变成不可变数组
        [muArr copy]

    3.NSString转换为NSArray
    //按照给定的字符串进行截取,将截取的多段字符串放入数组中
    - (NSArray *)componentsSeparatedByString:(NSString *)separator;

   // 例:有一个字符串,通过.进行分割
  NSString *string = @“www.lanou3g.com";
  NSArray *array = [string    componentsSeparatedByString:@"."];
 NSLog(@"%@", array);
输出结果:(
    www,
    lanou3g,
    com
)

    4.NSArray转换为NSString
   //将数组中的元素按照给定的字符串格式拼接成一个完整的字符串对象
   - (NSString *)componentsJoinedByString:(NSString *)separator;
//例:有一个数组,通过&将所有元素拼接成一个字符串
NSArray *array = @[@"北京", @"大连", @"河南", @"上海", @"广州", @"西安"];
NSString *string = [array componentsJoinedByString:@"&"];
NSLog(@"%@", string);
输出结果:北京&大连&河南&上海&广州&西安

字典

  • 1、不可变字典
    1.创建不可变字典
        NSDictionary * dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"one",@"1",@"two",@"2",nil];
        //快速创建字典
        NSDictionary * dic1 = @{@"3":@"three",@"4":@"four"};

    2.字典可以存储任意类型的对象
        NSArray * array = @[@"one",@"333"];
        NSNumber * num = [NSNumber numberWithInt:10];
        NSDicitonary * dic2 = @{@"dic":dic,@"num":num,@"array":array};

    3.获取字典的长度(键的个数)
        NSUInteger count = [dic2 count];

    4.从字典中取值
        NSString * arr = [dic3 objectForKey:@"array"];
        //快速取值
        NSDictionary * dic4 = dic3[@"dic"];
        NSNumber * number = dic3[@"num"];
  • 2、可变字典
    1.创建可变字典
    NSMutableDictionary * muDic = [[NAMutableDictionary alloc] initWithObjectsAndKeys:@"one",@"1",nil];
    //向可变字典中添加不可变字典
    NSDictionary * dic = @{@"3":@"three"};
    NSMutableDictionary * muDic2 = [[NSMutableDictionary alloc] initWithDictionary:dic];

    2.向字典中插入数据
    [muDic2 setObject:@"two" forKey:@"2"];

    3.遍历字典
    NSArray * allKeys = [muDic2 allKeys];
    for (id key in allKeys) {
        NSLog(@"%@",key);
        id obj = muDic2[key];
        NSLog(@"%@",obj);
    }

    4.删除数据
    [muDic2 removeObjectForKey:@"2"];

    5.全部删除
    [muDic removeAllObjects];

集合

//NSSet 是无序的,不能存储重复数据,可以用来去除重复数据
//NSArray 是自然顺序

  • 1、不可变集合
    1.创建
        NSSet * set = [[NSSet alloc] initWithObjects:@"one",@"b",@"two",@"three",@"a"@"two",nil];

    2.个数
        NSUInteger count = [set count];

    3.判断是否包含某个对象
        BOOL isContation = [set contationObject:@"t"];
        if (isContation) {
            NSLog(@"contation");
        } else {
            NSLog(@"not contation");
        }
  • 2、可变集合
    1.创建
        NSMutableSet * muSet = [[NSMutableSet alloc] initWithObjects:@"1",@"2",@"3",nil];

    2.添加对象
        [muSet addObject:@"four"];

    3.删除对象
        [muSet removeObject:@"2"];

    4.删除所有对象
        [muSet removeAllObjects];

相互转化

 1.数组-->集合
    NSSet * set = [[NSSet alloc] initWithArray:array];

 2.字典-->数组
    NSDictionary * dic = @{@"1":@"two",@"2":@"kk"};
    NSArray * keysArr = [dic allKeys];
    NSArray * valuaesArr = [dic allValues];

 3.字符串-->数组
    NSString * str = @"I am in shanghai";
    NSArray * strArr = [str componentsSeparatedByString:@" "];

 4.数组-->字符串
    NSString * str1 = [strArr componentsJoinedByString:@"-"];

你可能感兴趣的:(iOS数组,字典,集合)