本節(jié)學習內(nèi)容:
1.字典的概念
2.字典的創(chuàng)建及初始化
3.字典的操作
4.可變字典的創(chuàng)建及初始化
5.可變典的操作
【main.m】
#import<Foundation/Foundation.h>
【1.字典的概念】
//NSDictionary 創(chuàng)建不可變字典對象的類
//字典對象中的元素都是鍵值對统翩,key:value
//字典中的元素沒有順序
//NSMutableDictionary 創(chuàng)建可變字典對象凡怎,繼承于NSdictionary
//可以使用所有的不可變字黃類中的方法
//對于可變子典對象备埃,可以進行添加昼榛,修改刘莹,刪除操作
//在字典對象中罢艾,key的值是唯一抚恒,value的值可以相同
int main(int argc,const char*argv[]){
@autoreleasepool{
【2.字典的創(chuàng)建及初始化】
1.創(chuàng)建一個不可變對象
NSDictionary *dict=@{@"one":@"1",@"two":@"0"
,@"three":@"3",@"four":@"4"}
NSLog(@“dict=%@”,dict);
//打印結(jié)果= dict=(four=4;one=1,tree=3,two=2)
2.用傳入鍵值對創(chuàng)建值對初始化字典對象拄衰,key與值要相對應
NSDictionary *dict1=[[NSDictionar allic]initWithObjectsAndKeys:@"9",@"nine",@"eight",@"7",@"seven",nil];
NSLog[@"dict1=%@",dict1];
//打印結(jié)果: dict1=(eight=8;one=1,nine=9,seven=7)
3.用傳入的字典初始化字典
NSDictionary *dict2=[[NSDictionar alloc]initWithDictionary:dict];
NSLog[@"dict2=%@",dict2];
//打印結(jié)果: dict2=(four=4;one=1,tree=3,two=2)
4.用傳入值的數(shù)組,及傳入的key的數(shù)組創(chuàng)建字典對象鬼癣,要求值的數(shù)組與key的數(shù)組要對應
NSDictionary *dict3=[[NSDictionar alloc]initWithObjects:[NSArray arrayWithObjects:@"1",@"2",@"2",@"4",nil] forKeys:[NSArray arrayWithObjects:@"one",@"two",@"three",@"for",nil]];
NSLog[@"dict3=%@",dict3];
//打印結(jié)果: dict2=(four=4;one=1,tree=3,two=2)
5.創(chuàng)建空的字典對象
NSDictionary *dict4=[[NSDictionar alloc]init]
NSLog[@"dict4=%@",dict4];
//打印結(jié)果: dict4=()
5.類方法創(chuàng)建字典對象,創(chuàng)建空的字典對象
NSDictionary *dict5=[[NSDictionar dictionary];
NSLog[@"dict5=%@",dict5];
//打印結(jié)果: dict5=()
6.類方法用傳的鍵值創(chuàng)建字典對象
NSDictionary *dict6=[[NSDictionar dictionaryWithObjectsKeys:@"5",@"five",@"six",@"7",@"seven",nil];
NSLog[@"dict6=%@",dict6];
//打印結(jié)果: dict4=(five=5,seven=7,six=6)
7.類方法用傳的字典對象創(chuàng)建字典對象
NSDictionary *dict7=[[NSDictionar dictionaryWithDictionary:dict];
NSLog[@"dict7=%@",dict7];
//打印結(jié)果: dict7=(four=4;one=1,tree=3,two=2)
8.類方法 用傳的值數(shù)組與key數(shù)組 創(chuàng)建字典對象
NSDictionary *dict8=[NSDictionary dictionaryWithObjects:@"1",@"3",@"5",@"6"] forKeys:@[@"one",@"three",@"five",@"six"]]
NSLog[@"dict8=%@",dict8];
//打印結(jié)果: dict8=(five=5;one=1,six=6,three=3)
【3.字典的操作】
1.獲取字典對象中鍵值對的個數(shù)
NSInteger count=[dict count];
NSLog(@"count = %li",count);
//打印結(jié)果: count = 4
2.通過key獲取字典中key對應的值
id obj=[dict objectForKey:@"three"]
NSLog(@"obj = %@",obj);
//打印結(jié)果: obj = 3
3.獲取所有的values
NSArray *valueArray=[dict alValues];
NSLog(@"valueArray= %@",valueArray);
//打印結(jié)果: keyArray= (1,3,2,4)
3.獲取所有的key
NSArray *keyArray=[dict allkeys];
NSLog(@"keyArray= %@",keyArray);
//打印結(jié)果: keyArray= (one,three,two ,four)
4.判斷兩個字典對象是否相等
Bool ret=[dict isEqualToDictionary:@{@"one":@"1",@"two":@"2",@"three":@"3"}];
if(ret){
NSLog(@"字典對象相等");
}else{
NSLog(@"字典對象不相等");
}
//打印結(jié)果:字典對象不相等
5.字典的遍歷[枚舉器法]
NSEnumerator *keyEnumerator=[dic KeyEnumerator];
id obj2=nil;
while(obj2=[keyEnumerator nextObject]){
NSLog(@"key"=%@ value=%@",obj2,[dict objectForKey:obj2]);
}
//打印結(jié)果 key= one value=1,key= three value=3,key= two value=2, key= four value=4
6遍歷字典 [快速枚舉法]
for(id obj3 in dict){
NSLog(@"key"=%@ value=%@",obj3,[dict objectForKey:obj3]);
}
//打印結(jié)果 key= one value=1,key= three value=3,key= two value=2, key= four value=4
【4.可變字典的創(chuàng)建及初始化】
1.可變字典操作[構造指定容量大小的可變字典對象]
NSMutableDictionary *mulDict =[NSMutableDictionary alloc] initWitCapacity:20];
NSLog(@"mulDict = %@",mulDict);
//打印結(jié)果:mulDict=()
2.可變數(shù)組操作[類方法創(chuàng)建指定容量大小的可變字典對象]
NSMutableDictionary *mulDict1 =[NSMutableDictionary dictionaryWithCapacity:20];
NSLog(@"mulDict1= %@",mulDict1);
//打印結(jié)果:mulDict1=()
【5.可變字典的操作】
1.可變數(shù)組操作[刪除鍵值對]
NSMutableDictionary *mulDict3 =[NSMutableDictionary dictionaryWithDictionary:dict];
NSLog(@"mulDict3= %@",mulDict3);
2.可變數(shù)組操作[刪除指定key對應的鍵值對]
[mulDict3 removeObjectForKey:@"three"];
NSLog(@"mulDict3= %@",mulDict3);
//打印結(jié)果:mulDict3=(four=4; one=1;tow=2;)
3.可變數(shù)組操作[刪除key數(shù)組對應的鍵值對陶贼,只刪除出現(xiàn)在的key]
[mulDict3 removeObjectsForKey:@[@"one",@"two"]];
NSLog(@"mulDict3= %@",mulDict3);
//打印結(jié)果:mulDict3=(four=4;)
4.可變數(shù)組操作[刪除所有的鍵值對]
[mulDict3 removeAllobjects];
NSLog(@"mulDict3= %@",mulDict3);
//打印結(jié)果:mulDict3=()
5.添加鍵值對[把傳入字典對象的鍵值對添加到可變字典對象中]
[mulDict3 addEntriesFromDictionary:@{@"hello":"1",@"world":@"2",@"baidu":@"3",@"china":@"4"}];
NSLog(@"mulDict3= %@",mulDict3);
//打印結(jié)果:mulDict3=(baidu = 3; china = 4,hello = 1,world = 2);
6.重置可變字典對象方法
[mulDict3 setDictionary:dict];
NSLog(@"mulDict3= %@",mulDict3);
//打印結(jié)果:mulDict3=(four= 4; one= 1,three= 3,tow= 2);
7.添加或修改可變字典鍵值對
[mulDict3 setObject:@"5" forKey:@"five"];
NSLog(@"mulDict3= %@",mulDict3);
//打印結(jié)果:mulDict3=(five=5,four= 4; one= 1,three= 3,tow= 2);
//沒有就是添加,有就是修改
[mulDict3 setObject:@"10" forKey:@"one"];
NSLog(@"mulDict3= %@",mulDict3);
//打印結(jié)果:mulDict3=(five=5待秃;four= 4芬沉; one= 10;three= 3逸嘀;tow= 2);
}
return 0;
}