//
// main.m
// OC3_字典
//
// Created by zhangxueming on 15/6/12.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
//
#import <Foundation/Foundation.h>
//NSDictionary
//创建一个不可变字典对象
//NSMutableDictionary
//创建一个可变字典对象
//字典中对象都是键值对
//key:value
//字典中的键值对没有顺序
//字典中的key是唯一的, 但是value不一定是唯一的
int main(
int argc,
const char *
argv[]) {
@autoreleasepool {
//用value 及 key 构造字典对象
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:
@"1",
@"one",
@"2",
@"two",
@"3",
@"three",
@"4",
@"four",
@"4",
@"five",
@"4",
@"six", nil];
NSLog(@"dict = %@", dict);
//创建常量字典对象
NSDictionary *dict2 = @{
@"one":
@"1",
@"two":
@"2"};
NSLog(@"dict2 = %@", dict2);
//用传入的字典对象创建字典对象
NSDictionary *dict3 =
[[NSDictionary alloc] initWithDictionary:dict];
NSLog(@"dict3 = %@", dict3);
//用传入的value数组及key数组创建字典对象
NSDictionary *dict4 = [[NSDictionary alloc] initWithObjects:@[
@"3",
@"4",
@"5",
@"6"] forKeys:@[
@"three",
@"four",
@"five",
@"six"]];
NSLog(@"dict4 = %@", dict4);
//创建一个空的字典对象
NSDictionary *dict5 =
[[NSDictionary alloc] init];
NSLog(@"dict5 = %@", dict5);
//类方法创建字典对象
//跟initWithObjectsAndKeys:方法对应
NSDictionary *dict6 = [NSDictionary dictionaryWithObjectsAndKeys:
@"1",
@"one",
@"2",
@"two",
@"3",
@"three", nil];
NSLog(@"dict6 = %@", dict6);
//跟initWithObjects:forKeys:相对应
NSDictionary *dict7 = [NSDictionary dictionaryWithObjects:@[
@"3",
@"4",
@"5",
@"6"] forKeys:@[
@"three",
@"four",
@"five",
@"six"]];
NSLog(@"dict7 = %@", dict7);
//跟initWithDictionary:相对应
NSDictionary *dict8 =
[NSDictionary dictionaryWithDictionary:dict7];
NSLog(@"dict8 = %@", dict8);
//计算字典中键值对的个数
NSUInteger len =
[dict count];
NSLog(@"len = %li", len);
//获取key对应的值
id value = [dict objectForKey:
@"two"];
NSLog(@"value = %@", value);
//获取所有的key
NSArray *keys =
[dict allKeys];
NSLog(@"keys = %@", keys);
//获取值对应所有的key
NSArray *keys2 = [dict allKeysForObject:
@"4"];
NSLog(@"keys2 = %@", keys2);
//获取所有的值
NSArray *values =
[dict allValues];
NSLog(@"values = %@", values);
//判断两个字典是否相等
BOOL ret =
[dict isEqualToDictionary:dict2];
NSLog(@"ret = %i", ret);
//遍历字典 -- 实际上是遍历字典的key
//枚举器法
NSEnumerator *keysArray =
[dict keyEnumerator];
for (id item
in keysArray) {
NSLog(@"%@:%@",item,[dict objectForKey:item]);
}
//快速枚举法
for (id key
in dict) {
NSLog(@"%@:%@", key, [dict objectForKey:key]);
}
//只遍历字典中的值
NSEnumerator *valuesArray =
[dict objectEnumerator];
for (id item
in valuesArray) {
NSLog(@"vlaue = %@", item);
}
//可变字典操作
//构造指定容量大小的字典对象
//+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems;
NSMutableDictionary *mulDict = [[NSMutableDictionary alloc] initWithCapacity:
20];
NSLog(@"mulDict = %@",mulDict);
//删除指定的key对应的键值对
NSMutableDictionary *mulDict2 = [NSMutableDictionary dictionaryWithDictionary:@{
@"one":
@"1",
@"two":
@"2",
@"three":
@"3",
@"four":
@"4"}];
[mulDict2 removeObjectForKey:@"two"];
NSLog(@"mulDict2 = %@", mulDict2);
//添加(或者修改)键值对
[mulDict2 setObject:
@"5" forKey:
@"three"];
NSLog(@"mulDict2 = %@", mulDict2);
//将传入字典中的键值对添加到字典中 相同的修改,不同的就添加
[mulDict2 addEntriesFromDictionary:dict3];
NSLog(@"mulDict2 = %@", mulDict2);
//删除所有的键值对
[mulDict2 removeAllObjects];
NSLog(@"mulDict2 = %@", mulDict2);
//删除key数组对应所有键值对
NSMutableDictionary *mulDict3 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"5",
@"five",
@"6",
@"six",
@"7",
@"seven",
@"8",
@"eight", nil];
[mulDict3 removeObjectsForKeys:@[@"five",
@"eight",
@"nine"]];
NSLog(@"mulDict3 = %@", mulDict3);
//重置字典对象
[mulDict3 setDictionary:@{
@"hello":
@"1",
@"world":
@"3"}];
NSLog(@"mulDict3 = %@", mulDict3);
}
return 0;
}
转载于:https://www.cnblogs.com/0515offer/p/4572102.html