NSDictionary
1.NSDictionary初始化
1)字典中元素是键值对,所以字典元素都是成对出现的,一个元素就是一对(有key和对应的value)
2)键和值都是对象类型,键和值的类型可以不同
3)在字典中,不能出现相同的key,但可以出现相同的value,key的唯一性,这样就能够通过key唯一确定value
[Objective-C] 纯文本查看 复制代码 //不可变字典:字典对象一旦创建完成后,字典成员不能够被修改,增加和删除
NSDictionary *dictionary = @{@"key1":@"value1", @"key2":@"value2"};
//常用//初始化不可变字典//字典中的元素是成对出现的,要有key和value//key:value
#pragma mark 对象方法创建字典
NSDictionary *dictionary1 = [[NSDictionary alloc] initWithDictionary:dictionary];
//用其他字典初始化当前的字典对象
//字典中的key和value可以是任意的oc类型//nil:字典的结束标志
NSDictionary *dictionary2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1",
@"value2", @"key2", nil];
//value在前key在后,成对出现//最常用
#pragma mark 类方法创建字典
NSDictionary *dictionary3 = [NSDictionary dictionaryWithDictionary:dictionary];
NSDictionary *dictionary4 = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];
NSLog(@"dictionary===%@",dictionary);
NSLog(@"dictionary1===%@",dictionary1);
NSLog(@"dictionary2===%@",dictionary2);
NSLog(@"dictionary3===%@",dictionary3);
NSLog(@"dictionary4===%@",dictionary4);
2.NSDictionary的操作
[Objective-C] 纯文本查看 复制代码 // 字典中元素个数
NSInteger count = dictionary.count;
NSLog(@"dictionary的数量为%@",@(count));
// //根据key(索引)找value(值)
NSString *value1 = dictionary[@"key1"];
NSLog(@"dictionary中关键字key1对应的值为%@",value1);
//获取字典中的所有的key
NSArray *aryKey = [dictionary allKeys];
NSLog(@"字典dictionary的所有关键字是%@",aryKey);
//获取字典中所有的value
//第一种写法
for (NSString *key in [dictionary allKeys]) {
NSString *value = [dictionary objectForKey:key];
NSLog(@"关键字key为%@对应的值为%@",key,value);
}
//第二种写法
NSLog(@"dictionary的所有值为%@",[dictionary allValues]);
// 获取某个值value对应的所有键key
NSArray *keys = [dictionary allKeysForObject:@"value1"];
NSLog(@"dictionary值为value1对应的所有关键字为%@",keys);
打印结果:
NSMutableDictionary
1. NSMutableDictionary初始化
[Objective-C] 纯文本查看 复制代码 NSMutableDictionary *mdictionary = [NSMutableDictionary dictionaryWithCapacity:0];//创建一个空字典
2.NSDictionary的操作
[Objective-C] 纯文本查看 复制代码 //- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;
//作用:字典的末尾追加元素
[mdictionary setObject:@"苹果" forKey:@"name"];
[mdictionary setObject:@"脆甜的苹果" forKey:@"info"];
NSLog(@"末尾追加元素的mdictionary%@",mdictionary);
//- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;
//作用:字典末尾追加一个字典
NSDictionary *dic = @{@"egname": @"apple"};
[mdictionary addEntriesFromDictionary:dic];
NSLog(@"末尾了一个字典的mdictionary%@",mdictionary);
// 字典中元素的修改
//- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;
//作用:key值字典中已经存在,进行覆盖,如果不存在,追加
[mdictionary setObject:@"坏苹果" forKey:@"info"];
NSLog(@"修改字典mdictionary的键为info的值为坏苹果%@",mdictionary);
//- (void)setDictionary:(NSDictionary *)otherDictionary;
//作用:替换字典
NSDictionary *dic1 = @{@"name": @"apple"};
[mdictionary setDictionary:dic1];
NSLog(@"替换字典mdictionary为dic1%@",mdictionary);
// 字典中元素的删除(删除操作)
NSMutableDictionary *mdictionary1 = [NSMutableDictionary dictionaryWithCapacity:0];
[mdictionary1 setObject:@"香蕉" forKey:@"name"];
[mdictionary1 setObject:@"没熟的香蕉" forKey:@"info"];
//- (void)removeObjectForKey:(id)aKey;
//作用:根据key删除对应的元素
[mdictionary1 removeObjectForKey:@"info"];//常用
NSLog(@"删除字典mdictionary1中key为info的值%@",mdictionary1);
//删除所有的元素
[mdictionary1 removeAllObjects];
NSLog(@"字典mdictionary1删除了所有的值%@",mdictionary1);
运行结果:
附件:
Test-NSDictionary.zip
(155.42 KB, 下载次数: 0)
|