OC - 集合

来源:互联网 发布:网络女歌手排行榜甜美 编辑:程序博客网 时间:2024/06/05 11:15

数组

                // 实例方法初始化        NSArray *array = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",nil];        NSArray *array1 = [[NSArray alloc] initWithArray:array];        NSLog(@"array = %@,array1 = %@",array,array1);                //便利初始化方法        NSArray *array2 = [NSArray arrayWithObjects:@"3",@"4", nil];                //简化操作        NSInteger age = 10;        NSArray *array3 = @[@"5",@"6",@(age)];        NSLog(@"array3 = %@",array3);                //获取数组元素个数        NSUInteger count =  array3.count;        [array3 count];

数组的运算:

                // 实例方法初始化        NSArray *array = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",nil];        NSArray *array1 = [[NSArray alloc] initWithArray:array];        NSLog(@"array = %@,array1 = %@",array,array1);                //便利初始化方法        NSArray *array2 = [NSArray arrayWithObjects:@"3",@"4", nil];                //简化操作        NSInteger age = 10;        NSArray *array3 = @[@"5",@"6",@(age)];        NSLog(@"array3 = %@",array3);                //获取数组元素个数        NSUInteger count =  array3.count;        [array3 count];

数组的排序

        NSArray *array = @[@"12",@"3",@"6",@"2",@"1"];        NSArray *sortArray = [array sortedArrayUsingSelector:@selector(compare:)];        NSLog(@"sort = %@",sortArray);//        caseInsensitiveCompare,compare      NSArray *sortArray1 = [array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {            NSInteger a = [obj1 integerValue];            NSInteger b = [obj2 integerValue];            if (a > b) {                return -1;            }else if(a == b){                return 0;            }else{                return 1;            }        }];           NSLog(@"sort1 = %@",sortArray1);

数组的遍历

        NSArray *array = @[@"12",@"abcde",@6,@"2",@"1"];        //普通遍历        for (int i = 0; i < array.count ; i ++) {            NSLog(@"%@",array[i]);            NSLog(@"%@",[array objectAtIndex:i]);        }        //快速枚举        for (id object in array) {            NSLog(@"object = %@",object);            }

NSMutableArray

        NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"1",@"2", nil];        //增        [mutableArray addObject:@"3"];        NSLog(@"mutableArray = %@",mutableArray);        //删        [mutableArray removeObjectAtIndex:0];        NSLog(@"mutableArray = %@",mutableArray);        //查        NSInteger index = [mutableArray indexOfObject:@"2"];        NSLog(@"index = %ld",index);        //改(替换)        [mutableArray replaceObjectAtIndex:0 withObject:@"000"];        NSLog(@"mutableArray = %@",mutableArray);        //排序        [mutableArray sortedArrayUsingSelector:@selector(compare:)];        NSLog(@"mutableArray = %@",mutableArray);

iOS9 新特性

       NSArray<NSString *> *array = @[@"hello",@"world"];        NSMutableArray<NSString *> *mutableArray = [NSMutableArray arrayWithObjects:@"uu", nil];        [mutableArray addObject:@"3"];

字典NSDictionary

        //初始化        NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2", nil];        NSLog(@"dictionary = %@",dictionary);        //字典的快速生成        NSDictionary *newDictionary = @{                                        @"1" : @"one",                                        @"2" : @"two"                                        };        NSLog(@"dictionary = %@",newDictionary);        //键值对的个数        NSLog(@"%ld",dictionary.count);        //查询一个对象        NSString *string = dictionary[@"1"];        NSLog(@"%@",string);        NSString *string1 = [dictionary objectForKey:@"1"];        NSLog(@"%@",string1);        //取出所有的key        NSArray *keys = [dictionary allKeys];        //取出所有的对象       NSArray *values = [dictionary allValues];#pragma mark - 字典的遍历        //因为字典是以key-value形式存在,所以不可以直接遍历出一对,只能keys 或 value        //字典是无序的        //根据key,遍历所有的value        for (int i = 0; i < dictionary.count; i ++) {            NSString *key = [keys objectAtIndex:i];           // NSString *key = keys[i];            NSString *value = [dictionary objectForKey:key];           // NSString *value = dictionary[key];            NSLog(@"value = %@",value);        }        //快速枚举:遍历字典,保存的是key        for (id object in dictionary) {            NSLog(@"object = %@",dictionary[object]);        }

NSMutableDictionary

        //增:添加没有顺序        NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2", nil];        [mutableDictionary setValue:@"three" forKey:@"3"];        NSLog(@"mutable = %@",mutableDictionary);        //删        [mutableDictionary removeObjectForKey:@"3"];        NSLog(@"mutable = %@",mutableDictionary);        //查        NSLog(@"%@",[mutableDictionary objectForKey:@"1"]);        //改(根据key将原来的覆盖掉)        //setobject...forkey方法,如果字典中没有添加的这个Key,就做添加处理,如果有,当修改处理        [mutableDictionary setValue:@"three" forKey:@"1"];        NSLog(@"mutable = %@",mutableDictionary);        [mutableDictionary setObject:@"four" forKey:@"2"];        NSLog(@"mutable = %@",mutableDictionary);        //字典数组        NSDictionary *dictionary1 = @{   @"A": @"YES",                                        @"B":                                        @{@"math" : @"tom",                                       @"book" : @[@"english",@"chinese"]                                       }                                     };        NSLog(@"%@", dictionary1[@"B"][@"book"][0]);

NSSet

        NSSet *set = [NSSet setWithObjects:@"1",@"1",@"2",@"2", nil];        NSLog(@"set = %@",set);        //取出一个         NSLog(@"set1 = %@", [set anyObject]);        //取所有的对象          NSLog(@"set2 = %@",  [set allObjects]);        //里面的对象不能存放重复对象,主要用于去除重复数
0 0