天天看點

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);
        //類似于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