//數組與字典 :都是容器,只能存放對象
數組按順序存放對象,字典中的對象是無序的
在字典中可以通過key,快速找到該key所對應的value,
//創建字典 (在使用字典時,鍵一般式字符串,值可以是任意對象)
NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:@"one", @"1", @"two", @"2", @"three", @"3", nil];
NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"one", @"1", @"two", @"2", @"three", @"3", nil];
NSLog(@"dict = %@",dict);
//輸出 : dict = {1 = one;2 = two;3 = three;}
//遍歷字典中的數據(獲取字典的key)
for(id objin dict)
NSLog(@"obj = %@", obj);
//輸出: obj = 1, obj = 2, obj = 3
//根據key取到字典中對應的值
NSString *valueStr = [dict objectForKey: @"1"];
NSLog(@"valueStr = %@",valueStr);
//輸出: valueStr = one
//獲取字典中所有的key
NSArray *keys = [dict allKeys];
NSLog(@"keys = %@", keys);
//輸出 :keys = (1,2,3)
//獲取字典中所有的values
NSArray *values = [dict allValues];
NSLog(@"values = %@", values);
//輸出 : obj = one, obj = two, obj = three
for(id objin [dict allKeys])
NSLog(@"keys = %@, values = %@", obj, [dict objectForKey: obj]);
//輸出 : keys = 1, values = one keys = 2, values = two keys =3, values = three
//取出key對應的枚舉器
NSEnumerator *en = [dict keyEnumerator];
id obj1 = nil;
while (obj1 = [en nextObject]) {
NSLog(@"obj1 = %@", obj1);
}
//輸出: obj = 1, obj = 2, obj = 3