NSMutableDictionary是動態的是NSDictionary的子類,可以對所指向的記憶體區域內容進行更改,並可以增加陣列內容。
方法/步驟
//可以像NSDictionary一樣構建函式
NSMutableDictionary *mdic = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"one",@"1",@"two",@"2", nil];
//可變字典一般是建立空字典。
NSMutableDictionary *mDic1 = [[NSMutableDictionary alloc]init];
//新增字典的值鍵對
[mDic1 setObject:@"ONE" forKey:@"1"];
[mDic1 setObject:@"TWO" forKey:@"2"];
[mDic1 setObject:@"THREE" forKey:@"3"];
由於鍵是唯一的,但是最後複製給鍵的值會覆蓋該鍵之前所有的值
//刪除字典的鍵值對
[mDic1 removeObjectForKey:@"1"];
//列舉器進行遍歷字典
NSEnumerator *enum1 = [mDic1 objectEnumerator];
//複習NSMutableString
NSMutableString *mStr = [[NSMutableString alloc]init];
id str;
while (str = [enum1 nextObject]) {
[mStr appendString:str];
}
NSLog(@"%@",mStr);