Objective-C 字典:"NSDictionary" 和 "NSMutableDictionary"

时间:2022-10-24 19:36:15
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {

/*-------------------- NSDictionary ---------------------*/
//value在前,key 在后,value 和 key 要成双出现
//注意:在字典中键值是无序的



//创建一个空的字典
NSDictionary *dictionary1 = [[NSDictionary alloc] init];
NSDictionary *dictionary2 = [NSDictionary dictionary];

//创建一个键值对的字典
NSDictionary *dictionary3 = [NSDictionary dictionaryWithObject:@"1" forKey:@"One"];
NSLog(@"dictionary3 = %@",dictionary3);

//创建多个键值对的字典
NSDictionary *dictionary4 = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"a",@"2",@"b",@"3",@"c", nil];
NSLog(@"dictionary4 = %@",dictionary4);

NSDictionary *dictionary5 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"11",@"22",@"33", nil] forKeys:[NSArray arrayWithObjects:@"aa",@"bb",@"cc", nil]];
NSLog(@"dictionary5 = %@",dictionary5);

//根据一个字典创建一个新的字典
NSDictionary *dictionary6 = [NSDictionary dictionaryWithDictionary:dictionary5];
NSLog(@"dictionary6 = %@",dictionary6);

//本地读取字典
NSDictionary *dictionary7 = [NSDictionary dictionaryWithContentsOfFile:@""];

//从网络读取字典
NSDictionary *dictionary8 = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@""]];

//键值对的数量
NSUInteger count = [dictionary5 count];
NSLog(@"count = %lu",count);

//通过key来访问字典的值
NSDictionary *dictionary9 = [dictionary5 objectForKey:@"aa"];
NSLog(@"dictionary9 = %@",dictionary9);

//通过key来访问字典的值 新语法
NSDictionary *dictionary10 = dictionary5[@"bb"];
NSLog(@"dictionary10 = %@", dictionary10);


//如何得到key的值
NSArray *keysArray = dictionary5.allKeys;
NSLog(@"keysArray1 = %@",keysArray);

//如何得到value的值
NSArray *valuesArray = dictionary5.allValues;
NSLog(@"valuesArray = %@",valuesArray);


//遍历字典中的所有的值

//用for循环
for (int i = 0; i < valuesArray.count; i++) {
NSLog(@"%@ = %@",keysArray[i], dictionary5[keysArray[i]]);
}


//用 fori 快速枚举
for (NSString *key in dictionary5) {
NSLog(@"key = %@ value = %@", key, dictionary5[key]);
}


//使用枚举器遍历
NSEnumerator *enumrator = [keysArray objectEnumerator];
NSString *key;
while (key = [enumrator nextObject]) {
NSLog(@"%@ = %@",key, dictionary5[key]);
}


//在不可变字典中不能存储相同的key,如果相同的key,则只会答应一个key对应的value
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@25,@"age",@"GZ",@"address",@"Rick",@"name",@"Jack",@"name", nil];
for (NSString *key in [dictionary allKeys]) {
NSLog(@"%@ = %@", key, dictionary[key]);
}


/*------------------- NSMutableDictionary ---------------------*/

//初始化方法 创建一个又一个键值对的可变字典
NSMutableDictionary *mDictionary1 = [NSMutableDictionary dictionaryWithObject:@"zyx" forKey:@"name"];

//创建多个可变字典
NSMutableDictionary *mDictionary2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"zyx",@"name",@23,@"age",@"GZ",@"address", nil];
NSLog(@"mDictionary = %@",mDictionary2);

//通过另一个字典创建一个字典
NSMutableDictionary *mDictionary3 = [NSMutableDictionary dictionaryWithDictionary:mDictionary2];
NSLog(@"mDictionary3 = %@",mDictionary3);



//NSArray *array = mDictionary2.allKeys;
NSEnumerator *enumerator = [[mDictionary3 allKeys] objectEnumerator];
NSString *key1;
while (key1 = [enumerator nextObject]) {
NSLog(@"%@ = %@",key1, mDictionary3[key1]);
}


//枚举器
NSEnumerator *enumerator2 = [[mDictionary2 allKeys] objectEnumerator];
NSString *key2;
while (key2 = [enumerator2 nextObject]) {
NSLog(@"%@ = %@",key2 ,mDictionary2[key2]);
}

//可变字典独有初始化方法 创建了初始为10个键值对的可变字典,只是初始的大小,提高程序效率。并没有限制字典的大小,如果继续添加数据,字典元素个数会继续增加
NSMutableDictionary *mDictionary4 = [NSMutableDictionary dictionaryWithCapacity:10];

//操作字典
//增
[mDictionary4 setValue:@"zyx1" forKey:@"name1"];
[mDictionary4 setValue:@"zyx2" forKey:@"name2"];
[mDictionary4 setValue:@"zyx3" forKey:@"name2"];//(如果原来字典中有了Key这个关键字,这次新加Key,就会覆盖原来的Key)
//新语法
mDictionary4[@"name4"] = @"zyx4";
NSLog(@"mDictionary4 = %@",mDictionary4);

//删除指定key的对象
[mDictionary4 removeObjectForKey:@"name1"];
NSLog(@"%@",mDictionary4);

//删除全部
[mDictionary4 removeAllObjects];
NSLog(@"%@",mDictionary4);


//例子
NSMutableDictionary *mDictionary5 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"zyx",@"name1",@22,@"age",@"GZ",@"address",@"zyx",@"name2",@"zyx",@"name3", nil];

NSEnumerator *enumerator3 = [[mDictionary5 allKeys] objectEnumerator];
NSString *key3 ;

while (key3 = [enumerator3 nextObject]) {
NSLog(@"%@ = %@",key3, mDictionary5[key3]);
}

NSArray *key5 = mDictionary5.allKeys;
for (NSString *key in key5) {
NSString *string = mDictionary5[key];
if ([string isEqual:@"zyx"]) {
NSLog(@"key = %@",key);
}
//NSLog(@"%@ = %@",key, mDictionary5[key]);
}

}
return 0;
}