字典NSDictionary和NSMutableDictionary的使用

Posted 码出境界

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字典NSDictionary和NSMutableDictionary的使用相关的知识,希望对你有一定的参考价值。

简介:字典是一种数据结构,字典里面的每一个元素,是一个key-value(键值对),key和value都是对象类型。同NSArray一样,里面的对象不用保持一致性

NSDictionary

1、字面量初始化

// 存储时时无序的
NSDictionary *dict = @{@"name" : @"xiaoming", @"gendar" : @"man", @"age" : @"19"};
// 打印时根据Unicode码排序打印
NSLog(@"%@", dict);

2、实例方法初始化

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"19", @"age", @"man", @"gendar", @"xiaoming", @"name", nil];

3、类方法初始化

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"19", @"age", @"man", @"gendar", @"xiaoming", @"name", nil];

4、打印key-value的对数

 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"19", @"age", @"man", @"gendar", @"xiaoming", @"name", nil];
 NSLog(@"%@", dict.count);

注意:key-value键值对中的key具有唯一性,value可以相同。也就是说,栗子如下:

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"19", @"age", @"man", @"gendar", @"xiaoming", @"name", @"woman", @"gendar", nil];
NSLog(@"%@", dict);
====打印
{
    age = 19;
    gendar = man;
    name = xiaoming;
}
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"19", @"age", @"man", @"gendar", @"xiaoming", @"name", @"man", @"lastGendar", nil];
NSLog(@"%@", dict);
====打印
{
    age = 19;
    gendar = man;
    lastGendar = man;
    name = xiaoming;
}

5、根据key取出相应的value

NSDictionary *dict = @{@"name" : @"xiaoming", @"gendar" : @"man", @"age" : @"19"};


// valueForKey是NSDictionary的分类中的一个方法 NSString *nameString = [dict valueForKey:@"name"];

// objectForKey是NSDictionary本身定义的一个方法 NSString *ageString = [dict objectForKey:@"age"];

// 根据下标符取出value NSString *gendarString = dict[@"gendar"];
NSLog(
@"~~~~~~~~~~%@~~~~~~~~~~%@~~~~~~~~~~%@", nameString, ageString, gendarString); ====打印 ~~~~~~~~~~xiaoming~~~~~~~~~~19~~~~~~~~~~man

6、取出字典中所有的key、所有的value

NSDictionary *dict = @{@"name" : @"xiaoming", @"gendar" : @"man", @"age" : @"19"};
NSArray *keyArray = [dict allKeys];
NSArray *valueArray = [dict allValues];
NSLog(@"\n%@\n%@", keyArray, valueArray);
====打印
(
    name,
    gendar,
    age
)
(
    xiaoming,
    man,
    19
)

7、判断两个字典对象是否相同

NSDictionary *dict1 = @{@"name" : @"小明", @"gendar" : @"", @"age" : @"19"};
NSDictionary *dict2 = @{@"name" : @"小红", @"gendar" : @"", @"age" : @"19"};
BOOL bo = [dict1 isEqualToDictionary:dict2];
NSLog(@"%@", bo ? @"两者相同" : @"两者不相同");
====打印
两者不相同

NSMutableDictionary

1、构建方法初始化

NSMutableDictionary *mDict = [[NSMutableDictionary alloc] init];

2、类方法初始化

NSMutableDictionary *mDict = [NSMutableDictionary dictionary];

3、根据key值修改对应的value(找不到对应的key,就相当于添加这一对key-value)

NSMutableDictionary *mDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"19", @"age", @"man", @"gendar", @"xiaoming", @"name", @"woman",
@"gendar", nil];
[mDict setObject:@"20" forKey:@"age"];
[mDict setObject:@"China" forKey:@"address"];
NSLog(@"%@", mDict);
====打印
{
    address = China;
    age = 20;
    gendar = woman;
    name = xiaoming;
}

4、根据key删除key-value键值对

NSMutableDictionary *mDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"19", @"age", @"man", @"gendar", @"xiaoming", @"name", @"woman",
@"gendar", nil];
[mDict removeObjectForKey:@"name"];
NSLog(@"%@", mDict);
====打印
{
    age = 19;
    gendar = woman;
}

5、使用一个字典重置可变字典

 NSMutableDictionary *dict1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"19", @"age", @"man", @"gendar", nil];
 NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"20", @"age", @"woman", @"gendar", nil];
 [dict2 setDictionary:dict1];
 NSLog(@"%@", dict2);
 ====打印
{
    age = 19;
    gendar = man;
}

----未完待续

以上是关于字典NSDictionary和NSMutableDictionary的使用的主要内容,如果未能解决你的问题,请参考以下文章

iOS字典 setValue 和 setObject的区别

将 NSDictionary 转换为 Swift 字典

IOS开发-ObjC-NSDictionary

iOS开发--字典(NSDictionary)和JSON字符串(NSString)之间互转

JSON 和嵌套的 NSDictionary

OC第四节——NSDictionary和NSMutableDictionary