---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------
字典有不可变的:NSDictionary,可变的:NSMutableDictionary两种
字典的key------>value
索引------>文字内容
里面储存的都是键值对
一.NSDictionary
NSDictionary的创建:
1. NSDictionary *dict = [NSDictionary dictionaryWithObject: @"jack" forKey: @"name"];
2. NSArray *key = @[ @"name" ,@"adress" ];
NSArray *name = @[ @"jack" ,@"北京" ];
NSDictionary *dict1 = [NSDictionary dictionaryWithObjects: object forKeys: key ];
3. NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys: @"jack" ,@"name" ,@"北京", @"adress", nil];
4. NSDictionary *dict3 = @{@"name" : @"jack" , @"adress" : @"北京"};
二. NSMutableDictionary
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; //创建一个可变空的字典
[dict setObject : @"jack" forKey : @"name"]; //添加键值对
三. 字典的遍历
NSDictionary *dict3 = @{@"name" : @"jack" , @"adress" : @"北京"};
第一种方式:
NSArray *keys = [dict3 allKeys];
for( int i = 0 ; i<dict.count ; i++)
{
NSString *str =keys [ i ]; //遍历索引
NSString *str1 = dict3[keys]; //遍历文字内容
NSLog(@"%@= %@",str,str1);
}
第二种方式:
[dict3 emurateKeysObjectUsingBlock: //使用 block
^(id key ,id obj ,BOOL *stop )
{
NSLog(@"%@---%@",key , obj);
}];
--------------------- ASP.Net+Unity开发、 .Net培训、期待与您交流! ----------------------