oc75 不可變字典NSDictionary

2021-09-22 06:32:20 字數 1835 閱讀 6736

//

//nsdictionary

////

#import

int main(int argc, const

char *ar**)

//注意: key和value 是一一對應

nsdictionary *dict2 = [nsdictionary dictionarywithobjects:@[@"

lnj", @"

30", @"

1.75

"] forkeys:@[@"

name

", @"

age", @"

height

"]];

nslog(

@"dict2 = %@

", dict2); //

nslog(

@"%@ %@ %@

", [dict2 objectforkey:@"

name

"], [dict2 objectforkey:@"

age"], [dict2 objectforkey:@"

height

"]);

//nsdictionary *dict3 = @;

nsdictionary *dict4 = @;

nslog(

@"%@

", dict4[@"

name

"]);

nsdictionary *dict5 = @;

nslog(

@"dict5 = %@

", dict5);//

nslog(@"

%@ %@ %@

", dict5[@"

name

"], dict5[@"

age"], dict5[@"

height

"]);

//2.字典的遍歷

nsdictionary *dict6 = @;

如何獲取字典中key和value的個數, 在字典中key稱之為鍵, value稱之為值

nslog(@"

count = %lu

", [dict6 count]);

for (int i = 0; i < dict6.count; ++i)

//如何通過forin遍歷字典, 會將所有的key賦值給前面的obj

for (nsstring *key in

dict6)

[dict6 enumeratekeysandobjectsusingblock:^(id key, id obj, bool *stop) ];

//3.字典檔案讀寫

nsdictionary *dict7 = @;

//xml 副檔名plist

[dict7 writetofile:@"

/users/mctc/desktop/a.plist

"atomically:yes];

//注意: 字典和陣列不同, 字典中儲存的資料是無序的

nsdictionary *newdict8 = [nsdictionary dictionarywithcontentsoffile:@"

/users/mctc/desktop/a.plist"];

nslog(

@"%@

", newdict8);//

nsarray *arr9 = @[@10, @20, @30, @5

]; [arr9 writetofile:

@"/users/mctc/desktop/a.plist

"atomically:yes];

return0;

}

OC 字典(可變不可變都有 )

字典 nsdictionary 1.概念字典是關鍵字 key 及其定義的值 value 所構成的集合,即它是由key value組成的鍵值對的集合。key 一般是 nsstring 型別的,value 是oc中任意物件。對於字典來講,我們就是通過 key 來訪問 value 2.為什麼在有了陣列之後...

不可變字典和可變字典的誤區

突然想著set乙個值進去,為了方便點,在同乙個物件中拿值,結果竟然崩潰,百思不得其解,後來通過property的方式傳值過去。第二天早上仔細研究了下 原先的流程是一開始在didload裡面建立了可變字典 void viewdidload super viewdidload mtableview se...

OC不可變陣列NSArray詳解

1 nssarray 子類nsmutablearray nsset 子類nsmutableset nsdictionary 子類nsmutabledictionary 這幾個類都是集合類。2 c語言型別的陣列只能存放一種型別的資料。為此引入oc陣列nsarray,以物件導向的方法運算元據,可以存放各...