zoukankan      html  css  js  c++  java
  • NSDictionary NSMutableDictionary NSSet NSMutableSet

            //description只是返回了一个字符串
    //    [person description];
    //        //如果想要打印需要NSLog
    //    NSLog(@"%@", [person description]);
            //打印一个对象,实质打印的是description返回的字符串,如果没有重写description方法,系统默认打印<类名:指针[CDATA[]]>
    //    NSLog(@"%@", person);
        
    //    NSDictionary:不可变字典类,一旦被创建,不能修改:key-value(-)的形式存放数据
    //    name:mike age:18
        
            //创建
        
    //    NSDictionary *dic = [NSDictionary dictionaryWithObject:@"mike" forKey:@"name"];
    //    NSLog(@"%@", dic);
            //    name:mike age:18

        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"mike", @"name", @"18", @"age", nil];
    //    NSLog(@"%@", dic);
        
            //通过键,访问值
        [dic objectForKey:@"name"];

        NSLog(@"%@", [dic objectForKey:@"name"]);
        
        
    // name:mike age:18 name:helio age:16
    //    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"mike", @"name", @"18", @"age", @"helio", @"name1", @"18", @"age1", nil];
    //    NSLog(@"%@", dic);
        
    //    NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"mike", @"name", @"18", @"age", nil];
    //    NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"helio", @"name", @"18", @"age", nil];

        
            //字典中以key-value存储,
            //字典中key是唯一的,
            //字典中的key对应的value可以重复
            //创建具有多个键值对的字典时,nil结束
            //keyvalue不能为nil,会抛出异常
        
            //访问字典中的元素,和数组不同,不是用的下标,而是通过键值,访问值
            //    NSLog(@"%@", [dic objectForKey:@"name"]);
        
        
        
        
        
            //创建字典
        //name:xiaoming  age:18 gender: score:99.9
    //    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"xiaoming", @"name", @"18", @"age", @"", @"gender", @"99.9", @"score", nil];
    //    NSLog(@"%@", dic);
    //        //获取字典的键值对(实体)个数
    //    [dic count];
    //    NSLog(@"%lu", [dic count]);
    //
    //        //获取所有的key
    //    [dic allKeys];
    //    NSLog(@"%@", [dic allKeys]);
    //    
    //        //获取所有的value
    //    [dic allValues]; //    NSLog(@"%@", [dic allValues]); //   //        //通过key值获取对应的value值(由于key不能重复,找到的只有一个value) //     //    [dic objectForKey:@"name"]; //    NSLog(@"%@", [dic objectForKey:@"name"]); //    [dic objectForKey:@"age"]; //    [dic objectForKey:@"gender"]; //    [dic objectForKey:@"score"];                   //通过value值获取对应的key值(因为有相同value值得key有可能会有多个,返回的是一个数组)      //    NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"1", @"1", @"2", @"1", @"3", @"1", @"4", nil]; //    NSLog(@"%@", [dic1 allKeysForObject:@"1"]);                          //    NSMutableDictionary:可变字典 //    NSMutableDictionary *mDic = [[NSMutableDictionary alloc] initWithCapacity:0]; ////name:xiaoming  age:18 gender:男 score:99.9 //    [mDic setObject:@"xiaoming" forKey:@"name"]; //    [mDic setObject:@"18" forKey:@"age"]; //    [mDic setObject:@"男" forKey:@"gender"]; //    [mDic setObject:@"99.9" forKey:@"score"]; //    NSLog(@"%@", mDic); //    [mDic setObject:@"28" forKey:@"age"]; //    NSLog(@"%@", mDic);      //    setObject:forKey: //    如果key不存在,就把key-value存入字典 //    如果key已经存在,就修改key对应的value值              //删除一个键值对(实体);      //    [mDic removeObjectForKey:@"name"]; //    NSLog(@"%@", mDic);              //删除所有键值对 //    [mDic removeAllObjects]; //    NSLog(@"%@", mDic);                   //12     NSNumber *number = [[NSNumber alloc] initWithInt:12];     NSNumber *num2 = [NSNumber numberWithInt:12];     NSNumber *num3 = @12;              //1, 2, 3, 4 //    NSArray *aray1 = [[NSArray alloc] initWithObjects:@1, @2, @3, @4, nil]; //    NSArray *array2 = [NSArray arrayWithObjects:@1, @2, @3, @4, nil]; //    NSArray *array = @[@"1",@"2", @"3", @"4"];              //遍历 //    for (int i = 0; i < [array2 count]; i++) { //        NSLog(@"%@", array2[i]); //    }         //快速枚举 //    for (NSNumber *number in array2) { //        NSLog(@"%@", number); //    }      //    name:xiaoming  age:18 //    NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"xiaoming", @"name", @18, @"age", nil]; //    NSDictionary *dictionary1 = [NSDictionary dictionaryWithObjectsAndKeys:@"xiaoming", @"name", @18, @"age", nil]; //    NSDictionary *dictionary2 = @{@"name": @"xiaoming", @"age": @18}; //     //     //        //字典的遍历 //    NSArray *keys = [dictionary allKeys]; //    for (int i = 0; i < [dictionary count]; i++) { //        NSLog(@"%@ : %@", keys[i], [dictionary objectForKey:keys[i]]); //    } //     //     //        //快速枚举,打印字典 //        //对字典快速枚举,枚举的key //    for (NSString *key in dictionary1) { //        NSLog(@"%@: %@", key, [dictionary1 objectForKey:key]); //    }           //ios集合数据类型:NSArray,NSDictionary, NSSet      //    NSSet:无序的不可变集合           //    创建集合对象      //    NSSet *set = [[NSSet alloc] initWithObjects:@"a", @"b", @"c", nil]; ////     //    NSLog(@"%@", set); //     ////    获取元素个数 //     //    NSLog(@"%ld", [set count]); //     ////    获取集合中的某个元素 //    //    [set anyObject]; //     NSLog(@"%@", [set anyObject]); //     //    [set member:@"aaa"];//判断是否存在aaa存在就返回不存在返回nil //     ////    获取集合中是否包含某个对象 //     //  BOOL flag = [set containsObject:@"%@"];                       // NSMutableSet:无序的可变的集合类         //集合中的元素是不能重复的         //创建集合对象     NSMutableSet *mSet = [[NSMutableSet alloc] initWithCapacity:0];            //添加元素       [mSet addObject:@"aaa"];       [mSet addObject:@"bbb"];            NSLog(@"%@", mSet);         //删除元素            [mSet removeObject:@"aaa"];       NSLog(@"%@", mSet);              //删除所有集合       [mSet removeAllObjects];       NSLog(@"%@", mSet);

     
  • 相关阅读:
    小程序-小菊花loading
    小程序-跳转
    小程序-拨打电话
    Pluto
    Android外部存储
    iOS微信内存监控
    iOS UITableView左滑操作功能的实现(iOS8-11)
    Hi,腾讯 WeTest 限免开放 Android Oreo 云真机,Android 8.1 可开测!
    揭密微信跳一跳小游戏那些外挂
    腾讯WeTest发布《2017中国移动游戏质量白皮书》,专注手游品质提升
  • 原文地址:https://www.cnblogs.com/tian-sun/p/4309466.html
Copyright © 2011-2022 走看看