Oc课堂笔记->字典NSDictionary

来源:互联网 发布:arm java 指令集 编辑:程序博客网 时间:2024/05/22 12:40
字典

//创建一个字典,字典按照哈希排序排列的

    //第一种正常创建字典的初始化方法,不可变字典

    NSDictionary *dict1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"张三",@"name",@"男",@"sex",@"18",@"age",nil];

    NSLog(@"%@",dict1);

    //第二种创建字典方式,使用数组创建

    NSArray *keyArray = [NSArray arrayWithObjects:@"name",@"sex",@"age",nil];

    NSArray *vuleArray = [NSArray arrayWithObjects:@"张三",@"男",@"18",nil];

                         

    NSDictionary *dict2 = [[NSDictionary alloc] initWithObjects:vuleArray forKeys:keyArray];

    NSLog(@"%@",dict2);

    //第三种,使用字典创建字典

    NSDictionary *dict3 = [NSDictionary dictionaryWithDictionary:dict1];

    NSLog(@"%@",dict3);

    

    

    //常用方法----->取出所有key

    NSArray *arr1 = [dict3 allKeys];

    NSLog(@"%@",arr1);

    //------------>取出所有Values

    NSLog(@"%@",[dict3 allValues]);

    //------------>objectForKey根据指定的key值返回相应的Value值

    NSString *s1 =[dict3 objectForKey:@"name"];

    //id全是对象类型,所有类型不包括标量

    NSLog(@"%@",s1);

    //--------------->value:任意类类型的

    //--------------->key:能够当Key值的,一定遵循NSCopying协议

    Person * p1 = [[Person alloc] initWithName:@"lucy" age:18];

    Person * p2 = [[Person alloc] initWithName:@"lily" age:22];

    Person * p3 = [[Person alloc] initWithName:@"Tomy" age:37];

    NSArray *keyArray1 = [NSArray arrayWithObjects:[p1 name],[p2 name],[p3 name],nil];

    NSArray *valueArray1 = [NSArray arrayWithObjects:p1,p2,p3,nil];

    NSDictionary *dict4 = [[NSDictionary alloc] initWithObjects:valueArray1 forKeys:keyArray1];

    NSLog(@"%@",dict4);

    NSLog(@"++++++++%@",p1);    

    

    //可变字典

    NSMutableDictionary *mdict1 = [NSMutableDictionary dictionary];

    [mdict1 setObject:@"ad" forKey:@"21"];

    [mdict1 setObject:@"delete" forKey:@"21"];

    //添加元素

    [mdict1 setObject:@"abc" forKey:@"13"];

    NSLog(@"%@",mdict1);

    //删除元素

    [mdict1 removeObjectForKey:@"13"];

    NSLog(@"%@",mdict1);

    

    [mdict1 setObject:@"a" forKey:@"1"];

    [mdict1 setObject:@"b" forKey:@"2"];

    [mdict1 setObject:@"c" forKey:@"3"];

    NSLog(@"%@",mdict1);

    //修改:首先去找Key的值,看有没有Key值,

    //如果有,就用新值替换旧值,如果没有直接添加;

    [mdict1 setObject:@"x" forKey:@"1"];

    NSLog(@"%@",mdict1);

    

    

    */

  

  /*  Person *person1 = [[Person alloc] initWithName:@"tony" age:20];

    Person *person2 = [[Person alloc] initWithName:@"mike" age:18];

    Person *person3 = [[Person alloc] initWithName:@"joek" age:25];

    //初始化一个空字典

    NSMutableDictionary *mdict1 = [NSMutableDictionary dictionary];

    

    //添加字典元素

    //1.普通

    [mdict1 setObject:person1 forKey:[person1 name]];

    [mdict1 setObject:person2 forKey:[person2 name]];

    [mdict1 setObject:person3 forKey:[person3 name]];

    NSLog(@"%@",mdict1);

    //2.数组套字典

    NSMutableDictionary *mdict2 = [NSMutableDictionary dictionaryWithDictionary:mdict1];

    NSArray *arr1 = [NSArray arrayWithObjects:mdict1,mdict2,nil];

    NSDictionary *dict = [NSDictionary dictionaryWithObject:arr1 forKey:@"person"];

    

    NSLog(@"%@",dict);

    NSLog(@"%@",arr1);

    NSArray *arr2 = [dict valueForKey:@"person"];//取Key,外面是数组.用数组接收

    NSDictionary *dict2 = arr2[0];//取数组,数组外面是字典,所以用字典接受

    Person *p = [dict2 valueForKey:@"mike"];//取值,用person(父类)类型的接收

    NSLog(@"%@",p);

    //----------->不可变的集

    //集合(容器集合):包括:1,字典.2,数组,3,集(有时候网上叫集合错误的)

    //NSSet  集(hash排序)-------->{(叠起来一块的是集

    NSSet * set1 = [NSSet setWithObjects:@"1",@"2",@"3",@"3", nil];//集里面不能有重复的,如果有重复的,随机放一个

    NSLog(@"%@",set1);

    //使用

    //用来返回元素个数

    [set1 count];

    //随即返回一个对象,但是不保证是随机的,一般是第一个;

    NSString *s1 = [set1 anyObject];

    NSLog(@"%@",s1);

    NSSet *set2 = [NSSet setWithObjects:@"1",@"2",@"3",@"3", nil];

    //判断两个集是否相等

    BOOL b1 = [set1 isEqualTo:set2];

    NSLog(@"%d",b1);

    //判断传入对象是否在这个集中,在就返回出来,不在就返回空(null);

    NSString *s3 = [set1 member:@"04"];

    NSLog(@"++++%@",s3);

    

    //-------------->可变的集

    NSMutableSet *mset1 = [NSMutableSet set];

    //添加

    [mset1 addObject:@"32"];

    NSLog(@"%@",mset1);

    //删除

    [mset1 removeObject:@"32"];

    NSLog(@"%@",mset1);

    [mset1 setSet:set1];

    NSLog(@"%@",mset1);

    NSCountedSet *cset = [[NSCountedSet alloc] initWithObjects:@"a",@"b",@"c",@"a",@"b", nil];

    NSLog(@"%@",cset);


0 0