nsdictionary*dictionary1 = [[nsdictionary alloc]initwithobjectsandkeys:@"value0",@"key0",@"value1",@"key1",@"value2",@"key2",@"value3",@"key3",@"value4",@"key4",@"value4",@"key5",
nil];
nslog(@"%@",dictionary1);/**/nslog(@"%ld",dictionary1.count);/*5*/
//輸出特定元素
nslog(@"%@",dictionary1[@"key0"]);
nslog(@"%@",[dictionary1 objectforkey:@"key0"]);/*value0*/
//獲取所有key
nslog(@"%@",dictionary1.allkeys);/*(
key3,
key1,
key4,
key2,
key0
//獲取所有value
nslog(@"%@",dictionary1.allvalues);/*(
value3,
value1,
value4,
value2,
value0
//獲取特定value對應的所有key
nslog(@"%@",[dictionary1 allkeysforobject:@"value4"]);/*(
key4,
key5
//分別獲取key對應的value,對於不存在的key,返回"not found"
nslog(@"%@",[dictionary1 objectsforkeys:@[@"key0",@"key1",@"key8"] notfoundmarker:@"not found"]);/*(
value0,
value1,
"not found"
)*/nsdictionary*dictionaryother = @;
bool result=[dictionary1 isequaltodictionary:dictionaryother];
nslog(@"%hhd",result);/*0*/nsmutabledictionary
nsmutabledictionary繼承於nsdictionary,
在此基礎上新增加對元素新增、刪除、更新等操作,如下**演示:
nsmutabledictionary*mutabledictionary = [[nsmutabledictionary alloc]initwithcapacity:7];
nslog(@"%ld",mutabledictionary.count);/*0*/[mutabledictionary setvalue:@"value0" forkey:@"key0"];
[mutabledictionary setvalue:@"value1" forkey:@"key1"];
[mutabledictionary setvalue:@"value2" forkey:@"key2"];
[mutabledictionary setvalue:@"value3" forkey:@"key3"];
[mutabledictionary setvalue:@"value4" forkey:@"key4"];
[mutabledictionary setvalue:@"value5" forkey:@"key5"];
[mutabledictionary setvalue:@"value6" forkey:@"key6"];
[mutabledictionary setvalue:@"value7" forkey:@"key7"];
nslog(@"%ld",mutabledictionary.count);/*8*/nslog(@"%@",mutabledictionary);/*{
key0 = value0;
key1 = value1;
key2 = value2;
key3 = value3;
key4 = value4;
key5 = value5;
key6 = value6;
key7 = value7;
key8 = value8;
//刪除元素
[mutabledictionary removeobjectforkey:@"key7"];
nslog(@"%ld",mutabledictionary.count);/*7*/nslog(@"%@",mutabledictionary);/*{
key0 = value0;
key1 = value1;
key2 = value2;
key3 = value3;
key4 = value4;
key5 = value5;
key6 = value6;
//對於key存在,則修改對應的value,否則新增新元素
[mutabledictionary setvalue:@"update value6" forkey:@"key6"];
nslog(@"%@",mutabledictionary);//移除多個key對應的元素
[mutabledictionary removeobjectsforkeys:@[@"key6",@"key5"]];
nslog(@"%@",mutabledictionary);/*{
key0 = value0;
key1 = value1;
key2 = value2;
key3 = value3;
key4 = value4;
//移除所有元素
[mutabledictionary removeallobjects];
nslog(@"%ld",mutabledictionary.count);/*0*/
陣列反向遍歷ios iOS中遍歷的方法總結
在ios開發中,可以使用多種方法進行元素遍歷,具體有一下幾種 經典for迴圈 nsarray iosarray a b c d e f g for int i 0 i iosarray.count i 處理陣列中資料 nslog iosarray i nsenumerator遍歷 nsarray i...
1 遍歷陣列知識
1 public w3c dtd xhtml 1.0 transitional en 237 891014 15 1617 陣列定義 18 定義方式不同,陣列裡面可以放任何型別的資料 19 索引陣列的定義 20 attr array 1,2,3,4,aa 21 print r attr 22 關聯陣...
用陣列指標遍歷陣列,FOR FOREACH遍歷陣列
1.用陣列指標遍歷一維陣列 phpheader content type text html charset utf 8 用陣列指標遍歷一位陣列的值 arr array a b c d echo current arr 返回指標當前單元的值 echo next arr 指標引動到下乙個單元 echo...