nsarray *array1 = [nsarray arraywithobjects:@"iphone",@"ipod",nil];
nsarray *array2 = [nsarray arraywithobjects:@"mac",@"imac",@"mac pro",nil];
//類方法初始化自動釋放
nsdictionary *mydictionary = [nsdictionary dictionarywithobjectsandkeys:array1,@"mobile",array2,@"computers",nil];//注意用nil結束
nslog(@"mydictionary = %@",mydictionary);
int dictsize = [mydictionary count];
//訪問字典中的值
nsarray *mobile = [mydictionary objectforkey:@"mobile"];
//從乙個物件獲取鍵
nsarray *keys = [mydictionary allkeysforobject:array1];
//獲取字典中所有值得乙個陣列
nsarray *values = [mydictionary allvalues];
//快速列舉
for(id key in mydictionary)
//如果字典只包含屬性列表物件(nsdata,nsdate,nsnumber,nsstring,nsarray或nsdictionary)可以儲存到檔案中
bool success = [mydictionary writetofile:filepath atomically:yes];
//用檔案填充
nsdictionary *mydict2 =[nsdictionary dictionarywithcontentsoffile:filepath];
//可變字典
nsmutabledictionary *dictmutable = [[nsmutabledictionary alloc]initwithobjectsandkeys:array1,@"mobile",array2,@"computer", nil];
nsstring *string4 = @"stringtv";
//修改物件
[dictmutable setobject:string4 forkey:@"media"];
//刪除物件
[dictmutable removeobjectforkey:@"mobile"];
//刪除多個物件
nsarray *keyarray =[nsarray arraywithobjects:@"mobile",@"computer", nil];
[dictmutable removeobjectforkey:keyarray];
//刪除所有物件
[dictmutable removeallobjects];
NSDictionary 字典 KVC 的使用
字典分為不可變字典 nsdictionary 和可變字典 nsmutabledictionary 不可變字典 1.建立字典 nsdictionary dic nsdictionary dictionarywithobjectsandkeys male 20 age tom name run hobb...
字典NSDictionary 使用注意
void viewdidload self dic dic self dicdata nsdictionary alloc init void touchesbegan nsset touches withevent uievent event if a nslog e nslog a,b,c,d ...
NSDictionary的一些使用
1.這裡只有這兩個 如果乙個鍵 值對存在 setobject 這個方法就是 修改 如果乙個鍵 值對不存在 這個方法就是 增加。下面看個例子 判斷鍵值對存在與否是看鍵或者 值有乙個相同就是存在。1 nsstring last lastname 2 nsstring first firstname 3 ...