美文网首页
Objective-C数组字典集合(17-07-31)

Objective-C数组字典集合(17-07-31)

作者: Hilarylii | 来源:发表于2017-07-31 16:26 被阅读0次

    数组是一个有序集合,oc中只能存储对象类型。
    下标从0开始,通过下标访问数组元素。
    NSArray:创建好后,数组中的元素个数不能发生改变。
    NSMutableArray:可变数组.
    数组中多个元素用逗号隔开。

    //
    //  main.m
    //  OC04_数组字典集合
    //
    //  Created by lanou3g on 17/7/31.
    //  Copyright © 2017年 lanou3g. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    int main(int argc, const char * argv[]) {
        
        //数组:OC中的数组只能存储对象类型,必能存储int,float这些基础类型
        //不可变数组:不能改变元素个数
        NSArray *arr = [[NSArray alloc] initWithObjects:@"one",@"two",@"three", nil];
        NSArray *arr2 = [NSArray arrayWithObjects:@"one",@"two",@"three", nil];
        NSArray *arr3 = @[@"one",@"two",@"three"];
        //数组元素个数
        NSLog(@"%ld",arr.count);
        //从数组中取索引位置对应的元素
        NSLog(@"array中索引值为0处的元素:%@",[arr2 objectAtIndex:0]);
        //字面量形式,与上面方法等价
        NSLog(@"%@",arr3[0]);
        //判断数组是否包含一个对象
        if ([arr2 containsObject:@"two"]) {
            NSLog(@"包含");
        }else {
            NSLog(@"不包含");
        }
        //根据对象取索引值(首次出现的)
        NSLog(@"%ld",[arr2 indexOfObject:@"three"]);
        //将一个字符串分割
        NSString *string = @"www.baidu.com";
        NSArray *arr4 = [string componentsSeparatedByString:@"."];
        NSLog(@"%@",arr4);
        //将数组中的字符串合并
        NSArray *arr5 = @[@"北京",@"大连",@"哈尔滨",@"上海",@"广州",@"西安"];
        NSString *string1 = [arr5 componentsJoinedByString:@"&"];
        NSLog(@"%@",string1);
        
        NSString *string2 = @"http://www.imanhua.com/Cover/2011-10/hyrz.jpg&http://www.imanhua.com/Cover/2011-09/op.jpg&http://www.imanhua.com/Cover/2012-04/yjdwb.jpg";
        NSArray *arr6 = [string2 componentsSeparatedByString:@"&"];
        NSLog(@"%@",arr6);
        
        NSArray *arr7 = @[@"type = iOS", @"Device = iPhone", @"count = 11344115@163.com", @"password = 12345"];
        NSString *string3 = [arr7 componentsJoinedByString:@"&"];
        NSLog(@"%@",string3);
        
        //可变数组
        NSMutableArray *muarr1 = [[NSMutableArray alloc] initWithObjects:@"one",@"two",@"three", nil];
        //字面量创建方式(不可变数组拷贝一份可变的副本)
        NSMutableArray *muarr3 = [@[@"one",@"two"] mutableCopy];
        //由一个已知的数组创建另一个数组
        NSMutableArray *muArr1 = [NSMutableArray arrayWithArray:muarr3];
        
        NSMutableArray *muarr2 = [NSMutableArray arrayWithObjects:@"one",@"two",@"three", nil];
        //在数组的末尾处添加一个元素
        [muArr1 addObject:@"liying"];
        NSLog(@"%@",muArr1);
        
        [muArr1 insertObject:@"包子" atIndex:1];
        NSLog(@"%@",muArr1[1]);
        
        //移除一个对象(根据内存地址判定)
        [muArr1 removeObject:@"包子"];
        NSLog(@"%@",muArr1);
        
        //移除最后一个元素
        [muArr1 removeLastObject];
        NSLog(@"%@",muArr1);
        //通过索引位置去移除某个元素
        [muArr1 removeObjectAtIndex:1];
        NSLog(@"%@",muArr1);
        //清空数组
        [muArr1 removeAllObjects];
        NSLog(@"%@",muArr1);
        //修改某索引处的元素内容
        muArr1[0] = @"包包";
        NSLog(@"%@",muArr1);
        //交换两个索引处的元素
        [muarr1 exchangeObjectAtIndex:0 withObjectAtIndex:1];
        
        NSMutableArray *mua  =[NSMutableArray arrayWithObjects:@"one",@"two",@"three", nil];
        [mua addObject:@"five"];
        if ([mua containsObject:@"three"]) {
            NSUInteger index = [mua indexOfObject:@"three"];
            [mua replaceObjectAtIndex:index withObject:@"six"];
            NSLog(@"%@",mua);
            [mua removeObject:@"one"];
            NSLog(@"%@",mua);
        }
       
        [mua removeObject:@"two"];
        NSLog(@"%@",mua);
        
        //创建一个字典(key和value要求都是对象类型,key通常都是字符串,且key是唯一的,不允许两个相同的key出现)
        NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"包包",@"name",@"24",@"age", nil];//[值,键]
        NSString *name = [dic objectForKey:@"name"];
        NSLog(@"%@",name);
        //字面量写法
        NSDictionary *dic1 = @{@"name":@"olivia",@"age":@"99"};//[键,值]
        NSLog(@"%@",dic1);
        //字典里面取值(字面量写法)
        NSString *namee = dic[@"name"];
        NSLog(@"%@",namee);
        
        return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:Objective-C数组字典集合(17-07-31)

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