//老王小贴士快捷方式提高效率神器 @ + esc

    //可变的不建议使用

    NSArray *array = @[@"1",@"2"];

    NSDictionary *d = @{@"a": @"A", @"b": @"B"};

    //什么是字典//NSDictionary

    //保存键值对(key - value) 的容器

    //一个key 对应一个value(实体)

    //创建字典对象

    //类方法

    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"沐风", @"name", @"呵呵", @"sex", @"金紫荆韩式松骨", @"address", @"呵呵", @"exo", nil];

    //对象方法

    NSDictionary *dictionary1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"沐风", @"name", @"呵呵", @"sex", @"金紫荆韩式松骨", @"address", nil];

    //创建空字典

    NSDictionary *dictionaty2 = [NSDictionary dictionary];

    NSLog(@"%@", dictionary);

    //获取所有key

    NSLog(@"%@", dictionary.allKeys);//allkeys 属性

    //获取所有value

    NSLog(@"%@", dictionary.allValues);

    //根据keyvalue

    NSLog(@"%@", [dictionary objectForKey:@"name"]);

    //根据value查询key

    NSLog(@"%@", [dictionary allKeysForObject:@"呵呵"]);

    //字典中 一个key  只能对应一个value 但是一个value 可以对应多个key

    //间接遍历字典(根据allkeys数组获取字典中所有的key - value)

    for (NSString *key in dictionary.allKeys ) {

        NSLog(@"%@", [dictionary objectForKey:key]);

    }

    //NSMutableDictionary

    //初始化空的可变字典、

    NSArray *array = [NSArray array];

    NSMutableDictionary *mubDic = [NSMutableDictionary dictionary];

    //添加新的键值对

    [mubDic setObject:@"沐风" forKey:@"70"];

    [mubDic setObject:@"大汗" forKey:@"71"];

    NSLog(@"%@", mubDic);

    //修改key对应的value

    //本质是重新赋值

    [mubDic setObject:@"大汗" forKey:@"70"];

    [mubDic setObject:@"沐风" forKey:@"71"];

    //删除键值

    [mubDic removeObjectForKey:@"70"];

    NSLog(@"%@", mubDic);

    //删除多个键值


    [mubDic removeObjectsForKeys:(NSarray *)];

    //删除全部键值

    [mubdic removeAllObjects];


Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐