微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文链接http://www.cnblogs.com/wsq724439564/p/3283614.html

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);
        //类似于foreach
        for(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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐