ObjectC快速入门教程(3)---数组和字典

标签: Object-C
986人阅读 评论(0) 收藏 举报
分类:

一.不可变数组

1.创建

总结:OC中数组是对象的有序集合,数组存储的是对象的地址(对象的引用),不可变数组一旦被创建,不可以继续添加,删除,或者修改;数组中的元素可以为不同类型的对象.

  NSArray *array1 = @[@"one", @"two", @"three", @"four", [NSNumber numberWithInt:123]];
        NSLog(@"array1 = %@", array1);
        NSArray *array1 = @[@"one", @"two", @"three", @"four", [NSNumber numberWithInt:123]];
        NSLog(@"array1 = %@", array1);

        NSArray *array2 = [[NSArray alloc]init]; //空数组
        NSLog(@"array2 = %@", array2);

        NSArray *arryy3 = [[NSArray alloc]initWithObjects:@"one", @"two", nil];
        NSLog(@"array3 = %@", arryy3);

2.数组的访问

 NSLog(@"array1[1] = %@", [array1 objectAtIndex:1]);
        NSLog(@"array1[1] = %@", array1[1]);
        NSLog(@"cnt = %li", [array1 count]);

        //获取元素在数组中第一次出现的位置
        NSInteger index = [array1 indexOfObject:@"two"];
        NSLog(@"index = %li", index);

        NSLog(@"first = %@", [array1 firstObject]); //第一个元素
        NSLog(@"last = %@", [array1 lastObject]);   //最后一个元素

3.数组的比较

      //数组比较
        NSArray *array10 = @[@"one", @"two", @"three"];
        NSArray *array11 = [NSArray arrayWithObjects:@"one", @"two1", nil];
        BOOL ret = [array10 isEqualToArray:array11]; //判断两个数组是否相等
        if(ret){
            NSLog(@"两个数组相等");
        }else{
            NSLog(@"两个数组不想等");
        }
        //返回数组中第一个想等的元素
        id obj = [array10 firstObjectCommonWithArray:array11];
        NSLog(@"%@", obj);

4.数组排序

//数组排序,前提:数组中的元素是相同类型的
        NSArray *sortArray = [array10 sortedArrayUsingSelector:@selector(compare:)];//@selector大于0时进行交换
        NSLog(@"%@", sortArray);

5.数组添加

 //数组添加
        NSArray *array12 = [array10 arrayByAddingObject:@"five"];
        NSLog(@"array12 = %@", array12);

6.指定范围内的提取

 //提取指定范围内的数组
        NSArray *array13 = [array10 subarrayWithRange:NSMakeRange(2, 2)];
        NSLog(@"array13 = %@", array13);

        //NSIndexSet 下表集合类
        NSMutableIndexSet *index2 = [NSMutableIndexSet indexSetWithIndex:1];

        [index2 addIndex:3];
        NSArray *array14 = [array10 objectsAtIndexes:index2];
        NSLog(@"array14 = %@", array14);

7.字符串分割成数组

 NSString* str = @"I :am :good :boy";
        NSArray* array15 = [str componentsSeparatedByString:@" :"];
        NSLog(@"array15 = %@", array15);

        NSArray *array16 = [str componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" :"]];
        NSLog(@"array16 = %@", array16);

注意:componentsSeparatedByStringcomponentsSeparatedByCharactersInSet的区别,参考,前者以字符串整体作为分隔符,后者以字符串集合中的每一个字符作为分隔符.

8.数组元素的拼接

 NSArray *array = @[@"one", @"two", @"three"];
        NSString *arrayStr = [array componentsJoinedByString:@" "];
        NSLog(@"arrayStr = %@", arrayStr);

9.数组遍历

        //方法1,通过数组元素下标
        for(NSInteger i = 0; i < [array count]; i++){
//            NSLog(@"%@", [array objectAtIndex:i]);
        }

        //方法2,通过枚举器
        NSEnumerator *enumerator = [array reverseObjectEnumerator];
        id obj2 = nil;
        while(obj2 = [enumerator nextObject]){
//            NSLog(@"%@", obj2);
        }

        //方法3,快速枚举法
        for(id obj3 in array){
            NSLog(@"%@", obj3);
        }

二.可变数组

1.创建添加

NSMutableArray *marray = [[NSMutableArray alloc]initWithObjects:@"one", @"two", @"three", nil];
        [marray addObject:@"four"];
        [marray addObjectsFromArray:@[@"five", @"six"]]; //增加
        [marray insertObject:@"zero" atIndex:0];    //插入
        NSLog(@"%@", marray);

2.替换指定位置下标元素

  [marray setArray:@[@"1", @"2"]];
        NSLog(@"%@", marray);
        [marray replaceObjectAtIndex:1 withObject:@"123"];
        NSLog(@"%@", marray);

3.交换

   [marray exchangeObjectAtIndex:0 withObjectAtIndex:1];
        NSLog(@"%@", marray);

4.删除

三.字典对象

1.创建,添加

NSDictionary *dict0 = @{@"one":@"1",@"two":@"2"}; //前面是key后面是value
        NSLog(@"%@", dict0);
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"one",@"1",@"two", @"2",nil];//前面是value后面是key
        NSLog(@"%@", dict);

        NSMutableDictionary *dict1 = [NSMutableDictionary dictionaryWithDictionary:dict0];
        [dict1 setValue:@"4" forKey:@"four"]; //增加一个元素
        NSInteger count = [dict1 count]; //个数
        NSLog(@"%@", dict1);
        id obj33 = [dict1 objectForKey:@"one"]; //根据key得到value

        NSArray *keyArray = [dict1 allKeys]; //获得所有key
        NSArray *value = [dict1 allValues];  //获得所有value
        BOOL isEqual = [dict1 isEqualToDictionary:dict];
        if(isEqual){
            NSLog(@"相等");
        }else{
            NSLog(@"不相等");
        }

        NSLog(@"%@", value);

        //增加键值对
        [dict1 addEntriesFromDictionary:@{@"hello":@"hello123"}];

注意事项:在字典对象中,key的值是唯一的,一旦有了一个key,是不能覆盖的。

2.字典的遍历

    //方法1,枚举法
        NSEnumerator *keyEnumerator = [dict1 keyEnumerator];
        id obj44 = nil;
        while(obj44 = [keyEnumerator nextObject]){
            NSLog(@"key = %@ value = %@", obj44, [dict1 valueForKey:obj44]);
        }

        //方法2,快速枚举法
        for(id obj55 in dict1){
            NSLog(@"key = %@ value = %@", obj55, [dict1 objectForKey:obj55]);
        }
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5219373次
    • 积分:37379
    • 等级:
    • 排名:第111名
    • 原创:378篇
    • 转载:12篇
    • 译文:0篇
    • 评论:2981条
    博客专栏
    最新评论