您的位置:首页 > 其它

❀自我唠嗑OC-数组类,字典类,集合类

2015-12-21 20:15 211 查看
/——————————————–数组NSArray———————————————/

//一个有序集合,管理有序的组的对象时使用

[code]    //初始化
    NSArray *array1 = [[NSArray alloc]initWithObjects:@"A",@"B",@"C",@"D",nil];
    NSLog(@"array1=%@",array1);
    NSArray *array2 = [[NSArray alloc] initWithArray:array1];
    NSLog(@"array2=%@",array2);
    //便利构造器
    NSArray *array3 = [NSArray arrayWithObjects:@"w",@"x",@"y",@"z",nil];
    NSLog(@"array3=%@",array3);
    //语法糖
    NSArray *array = @[@"A",@"B",@"C"];
    NSLog(@"array=%@",array);


[code]    //获取数组内元素个数
    //- (NSUInteger)count;
    char count = [array1 count];
    NSLog(@"array1 count=%d",count);
    NSLog(@"%lu",[array1 count]);

    //返回指定索引的数组元素
    //- (id)objectAtIndex:(NSUInteger)index;
    NSLog(@"%@",[array1 objectAtIndex:[array1 count] - 2]);
    NSLog(@"%@",[array1 objectAtIndex:2]);
    NSLog(@"%@",array1[2]);

    //判断数组中是否包含某个给定对象
    //- (BOOL)containsObject:(id)anObject;
    NSLog(@"%@",[array1 containsObject:@"D"] ? @"YES" : @"NO");

    //追加对象
    //- (NSArray *)arrayByAddingObject:(id)anObject;
    NSLog(@"%@",[array1 arrayByAddingObject:@"F"]);

    //追加其他数组
    //- (NSArray *)arrayByAddingObjectsFromArray:(NSArray *)otherArray;
    NSArray *array4 = [array1 arrayByAddingObjectsFromArray:array3];
    NSLog(@"追加后为:%@",array4);
    //NSLog(@"%@",[array1 arrayByAddingObjectsFromArray:array3]);

    //获取数组中元素对象的下标
    //- (NSUInteger)indexOfObject:(id)anObject;
    NSUInteger uint = [array1 indexOfObject:@"C"];
    NSLog(@"下标:%lu",uint);

    //分割字符串存入数组
    //- (NSArray<NSString *> *)componentsSeparatedByString:(NSString *)separator;
    NSString *string = @"www.baidu.com";
    NSArray *array5 = [string componentsSeparatedByString:@"."];
    NSLog(@"%@",array5);

    //分割数组为字符串
    //- (NSString *)componentsJoinedByString:(NSString *)separator;
    NSString *string1 = [array1 componentsJoinedByString:@","];
    NSLog(@"%@",string1);
    //NSLog(@"%@",[array1 componentsJoinedByString:@","]);


/————————————可变数组NSMutableArray————————————/

[code]    //初始化
    NSMutableArray *MutableArray = [[[NSMutableArray alloc] initWithObjects:@"blue",@"gray",@"green",nil] autorelease];
    //便利构造器
    NSMutableArray *MutableArray1 = [NSMutableArray arrayWithCapacity:0];
    //语法糖
    NSMutableArray *MutableArray2 = [@[@"January",@"February",@"March",@"April"] mutableCopy];


[code]    //添加对象
    //- (void)addObject:(id)anObject;
    [MutableArray addObject:@"sky"];
    NSLog(@"%@",MutableArray);

    //指定索引位置插入对象
    //- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
    [MutableArray insertObject:@"red" atIndex:1];
    NSLog(@"%@",MutableArray);

    //移除最后一个对象
    //- (void)removeLastObject;
    [MutableArray removeLastObject];
    NSLog(@"%@",MutableArray);

    //移除指定索引位置对象
    //- (void)removeObjectAtIndex:(NSUInteger)index;
    [MutableArray removeObjectAtIndex:1];
    NSLog(@"%@",MutableArray);

    //替换指定索引位置对象
    //- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
    [MutableArray replaceObjectAtIndex:1 withObject:@"black"];
    NSLog(@"%@",MutableArray);

    //销毁数组
    //- (void)removeAllObject;
    [MutableArray removeAllObjects];
    NSLog(@"%@",MutableArray);

    //交互指定索引之间的对象
    //- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;
    [MutableArray2 exchangeObjectAtIndex:0 withObjectAtIndex:2];
    NSLog(@"%@",MutableArray2);

    //移除指定对象
    //- (void)removeObject:(id)anObject;
    [MutableArray2 removeObject:@"April"];
    NSLog(@"%@",MutableArray2);

    //移除指定区域指定对象,根据对象isEqual消息判断
    //- (void)removeObject:(id)anObject inRange:(NSRange)range;
    [MutableArray2 removeObject:@"March" inRange:NSMakeRange(0, 2)];
    NSLog(@"%@",MutableArray2);

    //指定(数组B的指定区域)的元素替换(数组A的指定区域)
    //- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray range:(NSRange)otherRange;
    NSMutableArray *MutableArray3 = [NSMutableArray arrayWithObjects:@"May",@"June",@"July",nil];
    [MutableArray2 replaceObjectsInRange:NSMakeRange(0, 1) withObjectsFromArray:MutableArray3 range:NSMakeRange(0, 2)];
    NSLog(@"%@",MutableArray2);

    //追加数组A到数组B
    //- (void)addObjectsFromArray:(NSArray *)otherArray;
    NSMutableArray *MutableArray4 = [NSMutableArray arrayWithObjects:@"August", nil];
    [MutableArray2 addObjectsFromArray:MutableArray4];
    NSLog(@"%@",MutableArray2);


[code]//OC冒泡排序
    NSMutableArray *muArray = [NSMutableArray array];
    for (int i = 0; i < 10; i++) {
        NSNumber *num = [NSNumber numberWithInteger:arc4random() % 90];
        [muArray addObject:num];
    }
    for (int i = 0; i < [muArray count] - 1; i++) {
        for (int j = 0; j < [muArray count] - 1 - i; j++) {
            if ([muArray[j] integerValue] > [muArray[j + 1] integerValue]) {
                [muArray exchangeObjectAtIndex:j withObjectAtIndex:j + 1];//直接将索引对象交换
            }
            //方法2
//            if (muArray[j] > muArray[j + 1]) {
//                int a = [muArray [j] intValue];
//                int b = [muArray[j + 1] intValue];
//                if (a > b) {
//                    NSString *max = muArray[j];
//                    muArray[j] = muArray[j + 1];
//                    muArray[j + 1] = max;
//                }
//            }
        }
    }
    NSLog(@"%@",muArray);


/—————————————-字典NSDictionary——————————————/

//无序集合,存储对象为各个键值对,获取数据时需使用对应的key操作,一个value 对应一个key

[code]    //字典初始化
    NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"apple",@"name",@"pear",@"name1",@"melon",@"name2",@"grape",@"name3", nil];
    //便利构造器
    NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"apple",@"name",@"pear",@"name1",@"melon",@"name2",@"grape",@"name3", nil];
    //字面量(反人类没必要用)
    NSDictionary *dic2 = @{@"key1":@"apple",@"key2":@"pear"};


[code]    //获得所有的key
    NSArray *keyArray = [dic allKeys];
    NSLog(@"%@",keyArray);
    //获得所有value
    NSArray *valueArray = [dic1 allValues];
    NSLog(@"%@",valueArray);
    //通过key找到value
    NSLog(@"%@",[dic objectForKey:@"name3"]);


/——————————-可变字典NSMutableDictionary———————————-/

[code] //初始化
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"apple",@"name",@"pear",@"name1",@"melon",@"name2",@"grape",@"name3", nil];
    //向词典中动态添加数据,key相同则替换掉value,如没相同key则添加value;
    //-(void)setObject:(id)anObject forKey:(id<NSCopying>)akey
    //NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    [dictionary setObject:@"lemon" forKey:@"name"];
    NSLog(@"%@",dictionary);

    //通过key移除某个value
    //- (void)removeObjectForKey:(id)aKey;
    [dictionary removeObjectForKey:@"name2"];
    NSLog(@"%@",dictionary);

    //销毁所有字典所有数据
    //- (void)removeAllObjects;
    [dictionary removeAllObjects];
    NSLog(@"%@",dictionary);


/———————————————集合NSSet———————————————-/

//无序容器,容器中对象不能重复,不可变集合一旦创建,集合中的对象无法修改,只能从集合中读取对象,并且没有快速创建集合对象的字面量

[code] //初始化
    NSSet *nam = [[NSSet alloc] initWithObjects:@"apple",@"pear",@"grape", nil];
    //便利构造器
    NSSet *nam1 = [NSSet setWithObjects:@"apple",@"pear",@"grape", nil];


[code] //获取集合中对象的个数
    //@property (readonly) NSUInteger count;
    NSLog(@"%lu",[nam count]);

    //获取集合中所有的对象
    //@property (readonly, copy) NSArray
    //*allObjects;
    NSLog(@"%@",[nam allObjects]);

    //从集合获取任意一个对象
    //- (id)anyObject;
    NSLog(@"%@",[nam1 anyObject]);

    //判断集合中是否包含某一个指定的对象
    //- (BOOL)containsObject:(id)anObject;
    NSLog(@"contain:%d",[nam1 containsObject:@"grape"]);

    //判断两个集合是否匹配
    NSLog(@"isEqual:%d",[nam1 isEqual:nam]);


/—————————————-NSMutableSet创建—————————————-/

[code]    //初始化方法
    //NSMutableSet *name = [[NSMutableSet alloc] initWithCapacity:0];
    //便利构造器
    //NSMutableSet *name1 = [NSMutableSet setWithCapacity:0];

    NSMutableSet *color = [[NSMutableSet alloc] initWithObjects:@"red",@"yellow",@"blue", nil];

    //添加一个对象
    //- (void)addObject:(id)object;
    [color addObject:@"green"];
    NSLog(@"%@",color);
    //NSLog(@"%@",[color addObject:@"green"]);

    //移除一个对象
    //- (void)removeObject:(id)object;
    [color removeObject:@"red"];
    NSLog(@"%@",color);

    //移除所有对象
    //- (void)removeAllObjects;
    [color removeAllObjects];
    NSLog(@"%@",color);


❀❀三类写法大同小异,运用十分频繁,加强练习提高熟练程度❀❀
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: