日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Objective-C ,ios,iphone开发基础:NSDictionary(字典) 和 NSMutableDictionary

發布時間:2025/7/14 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Objective-C ,ios,iphone开发基础:NSDictionary(字典) 和 NSMutableDictionary 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

NSDictionary(字典),NSDictionary類似于 .net中的parameter,l類似于java中的map。

通過唯一的key找到對應的值,一個key只能對應一個只,而多個key可以對應同一個值。NSDictionary 在初始化之后,就不可以再進行修改。

使用類方法創建NSDictionary對象。

?

初始化一個NSDictionary對象。使用+ (id)dictionaryWithObject:(id)object forKey:(id)key;

NSDictionary* dic = [NSDictionary dictionaryWithObject:@"values1" forKey:@"key1"]; NSLog(@"%@",dic);
//結果

2013-08-26 19:13:29.274 Nsdictonary[288:707] {
key1 = values1;
}

初始化一個NSDictionary對象。使用+ (id)dictionaryWithObjectsAndKeys:(id)firstObject, ... NS_REQUIRES_NIL_TERMINATION;

NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:@"values1",@"key1"@"values2",@"key2"@"values3",@"key3" ,nil];NSLog(@"%@",dic);、 //結果

初始化一個NSDictionary對象。使用+ (id)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;

NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];NSDictionary* dic = [NSDictionary dictionaryWithObjects:values forKeys:keys]; NSLog(@"%@",dic);

結果:

2013-08-26 19:30:34.286 Nsdictonary[345:707] {
key1 = values1;
key2 = values2;
key3 = values3;
}

使用實例方法創建NSDictionary

創建一個空的字典:

NSDictionary* dic = [[NSDictionary alloc]init];NSLog(@"%@",dic);
[dic release];

通過兩個數組創建字典對象。

NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];NSDictionary* dic = [[NSDictionary alloc] initWithObjects:values forKeys:keys];NSLog(@"%@",dic);

通過一個字典來創建一個新的字典。

NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];NSDictionary* dic2 = [NSDictionary dictionaryWithObjects:values forKeys:keys];NSDictionary* dic = [[NSDictionary alloc] initWithDictionary:dic2];NSLog(@"%@",dic);

計算一個字典中有多少個鍵值對:

NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];NSDictionary* dic2 = [NSDictionary dictionaryWithObjects:values forKeys:keys];NSDictionary* dic = [[NSDictionary alloc] initWithDictionary:dic2];NSLog(@"count :%lu",[dic count]);NSLog(@"%@",dic1);

結果:

2013-08-26 19:44:54.809 Nsdictonary[439:707] count :3
2013-08-26 19:44:54.817 Nsdictonary[439:707] {
key1 = values1;
key2 = values2;
key3 = values3;
}

通過健來去對應的值:

NSObject* obj = [dic objectForKey:@"key1"]; NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];NSDictionary* dic2 = [NSDictionary dictionaryWithObjects:values forKeys:keys];NSDictionary* dic = [[NSDictionary alloc] initWithDictionary:dic2];NSObject* obj = [dic objectForKey:@"key1"];NSLog(@"key1 = %@",obj);
結果:
2013-08-26 19:47:24.175 Nsdictonary[453:707] key1 = values1
將字典寫入文件中: [dic writeToFile:path atomically:YES];
NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];NSDictionary* dic2 = [NSDictionary dictionaryWithObjects:values forKeys:keys];NSDictionary* dic = [[NSDictionary alloc] initWithDictionary:dic2];NSString* path =@"/Users/administrator/Desktop/test.xml";NSLog(@"dic:%@",dic);[dic writeToFile:path atomically:YES];NSDictionary* dicTest = [NSDictionary dictionaryWithContentsOfFile:path];NSLog(@"dicTest: %@",dicTest);

結果:

2013-08-26 19:55:31.276 Nsdictonary[500:707] dic:{
key1 = values1;
key2 = values2;
key3 = values3;
}
2013-08-26 19:55:31.294 Nsdictonary[500:707] dicTest: {
key1 = values1;
key2 = values2;
key3 = values3;
}

返回所有的keys:

NSArray* retKeys = [dic allKeys];: NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];NSDictionary* dic2 = [NSDictionary dictionaryWithObjects:values forKeys:keys];NSDictionary* dic = [[NSDictionary alloc] initWithDictionary:dic2];NSArray* retKeys = [dic allKeys];NSLog(@"all keys :%@",retKeys);

結果:

2013-08-26 19:58:48.871 Nsdictonary[515:707] all keys :(
key1,
key3,
key2
)

返回所有的值:allvalues

NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];NSDictionary* dic2 = [NSDictionary dictionaryWithObjects:values forKeys:keys];NSDictionary* dic = [[NSDictionary alloc] initWithDictionary:dic2];NSArray* retValues = [dic allValues];NSLog(@"all keys :%@",retValues);

結果:

2013-08-26 19:59:57.768 Nsdictonary[532:707] all keys :(
values1,
values3,
values2
)

NSMutableDictionary ?創建插入刪除

?

創建一個

NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys];NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:dic2];NSLog(@"dic : %@",dic);
結果:

2013-08-26 20:11:56.388 Nsdictonary[634:707] dic : {
key1 = values1;
key2 = values2;
key3 = values3;
}

?

插入一個新的健值對:

[dic setObject:@"values4" forKey:@"key4"]; NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys];NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:dic2];NSLog(@"dic : %@",dic);[dic setObject:@"values4" forKey:@"key4"];NSLog(@"dic : %@",dic);
結果:

2013-08-26 20:15:36.330 Nsdictonary[680:707] dic : {
key1 = values1;
key2 = values2;
key3 = values3;
}
2013-08-26 20:15:36.338 Nsdictonary[680:707] dic : {
key1 = values1;
key2 = values2;
key3 = values3;
key4 = values4;
}

移除一個健值對:

[dic removeObjectForKey:@"key1"]; NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys];NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:dic2];NSLog(@"dic : %@",dic);[dic removeObjectForKey:@"key1"];NSLog(@"dic : %@",dic);
結果:

2013-08-26 20:17:33.980 Nsdictonary[695:707] dic : {
key1 = values1;
key2 = values2;
key3 = values3;
}
2013-08-26 20:17:34.013 Nsdictonary[695:707] dic : {
key2 = values2;
key3 = values3;
}

移除所有健值對:

removeAllObjects NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys];NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:dic2];NSLog(@"dic : %@",dic);[dic removeAllObjects];NSLog(@"dic : %@",dic);
結果:

2013-08-26 20:18:38.027 Nsdictonary[711:707] dic : {
key1 = values1;
key2 = values2;
key3 = values3;
}
2013-08-26 20:18:38.036 Nsdictonary[711:707] dic : {
}

遍歷字典: for(id objects in dic)
NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys];NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:dic2];NSLog(@"dic : %@",dic);//類似于foreachfor(id objects in dic){NSObject* obj = [dic objectForKey:objects];NSLog(@"%@ = %@",objects,obj);}
結果:

2013-08-26 20:24:00.303 Nsdictonary[757:707] dic : {
key1 = values1;
key2 = values2;
key3 = values3;
}
2013-08-26 20:24:00.353 Nsdictonary[757:707] key1 = values1
2013-08-26 20:24:00.362 Nsdictonary[757:707] key3 = values3
2013-08-26 20:24:00.371 Nsdictonary[757:707] key2 = values2

?

迭代器遍歷字典:

NSEnumerator* em = [dic keyEnumerator]; NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys];NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:dic2];NSLog(@"dic : %@",dic);NSEnumerator* em = [dic keyEnumerator];id key =nil;while(key = [em nextObject]){NSObject* obj = [dic objectForKey:key];NSLog(@"%@ = %@",key,obj);}
結果:

2013-08-26 20:28:23.753 Nsdictonary[771:707] dic : {
key1 = values1;
key2 = values2;
key3 = values3;
}
2013-08-26 20:28:23.871 Nsdictonary[771:707] key1 = values1
2013-08-26 20:28:23.873 Nsdictonary[771:707] key3 = values3
2013-08-26 20:28:23.879 Nsdictonary[771:707] key2 = values2

?

block遍歷字典:

[dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {NSLog(@"%@ = %@",key,obj);}]; NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys];NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:dic2];NSLog(@"dic : %@",dic);[dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {NSLog(@"%@ = %@",key,obj);}];
結果:

2013-08-26 20:32:09.894 Nsdictonary[789:707] dic : {
key1 = values1;
key2 = values2;
key3 = values3;
}
2013-08-26 20:32:09.906 Nsdictonary[789:707] key1 = values1
2013-08-26 20:32:09.913 Nsdictonary[789:707] key3 = values3
2013-08-26 20:32:09.917 Nsdictonary[789:707] key2 = values2

?

?

?

?

?

?

?

?

?

?

?

?

?

?

轉載于:https://www.cnblogs.com/wsq724439564/p/3283614.html

總結

以上是生活随笔為你收集整理的Objective-C ,ios,iphone开发基础:NSDictionary(字典) 和 NSMutableDictionary的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。