四、NSDictionary和NSMutableDictionary类
//____________________NSDictionary_________________
// 创建字典
NSNumber *number = [NSNumber numberWithInt:100];
// 初始化一个元素
NSDictionary *dic = [NSDictionary dictionaryWithObject:number forKey:@"key"];
NSLog(@"dic : %@", dic);
// 初始化两个元素
NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", @"v3", @"k3", @"v4", @"k4", @"v5", @"k5", nil];
NSLog(@"dic1 : %@", dic1);
// 用一个已存在的dic来初始化另一个dic
NSDictionary *dic2 = [NSDictionary dictionaryWithDictionary:dic];
NSLog(@"dic2 : %@", dic2);
// 取字典的数量
int count = [dic1 count];
NSLog(@"count : %d", count);
// 根据指定的key取value
NSString *value = [dic1 valueForKey:@"k2"];
NSLog(@"value for key k2 : %@", value);
// 获取所有键的集合
NSArray *keys = [dic1 allKeys];
NSArray *values = [dic1 allValues];
NSLog(@"keys : %@", keys);
NSLog(@"values : %@", values);
///
// 可变字典 继承自NSDictionary,所以上面NSDictionary的方法它都有
// 初始化一个空的可变字典
NSMutableDictionary *mutableDic = [NSMutableDictionary dictionary];
NSLog(@"mutableDic = %@", mutableDic);
// 用一个元素初始化
NSMutableDictionary *mutableDic2 = [NSMutableDictionary dictionaryWithObject:@"value" forKey:@"key"];
NSLog(@"mutableDic2 : %@", mutableDic2);
// 用多个元素初始化
NSMutableDictionary *mutableDic3 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", @"v3", @"k3", @"v4", @"k4", @"v5", @"k5", nil];
NSLog(@"mutableDic3 : %@", mutableDic3);
// 向字典中添加整个字典对象
[mutableDic3 addEntriesFromDictionary:mutableDic2];
NSLog(@"mutableDic3 : %@", mutableDic3);
// 向字典中添加一对新的key和value
[mutableDic3 setValue:@"v6" forKey:@"k6"];
NSLog(@"mutableDic3 : %@", mutableDic3);
// 将字典1设置成和字典2的内容一样 注意,字典1原先的内容会被覆盖掉
[mutableDic2 setDictionary:mutableDic3];
NSLog(@"mutableDic2 : %@", mutableDic2);
// 根据key删除键值对
[mutableDic2 removeObjectForKey:@"key"];
NSLog(@"mutableDic2 : %@", mutableDic2);
/
// 字典的遍历
// 效率低法
NSArray *keys1 = [mutableDic2 allKeys];
for (int i = 0; i < [mutableDic2 count]; i++) {
NSString *key = [keys1 objectAtIndex:i];
NSString *value = [mutableDic2 valueForKey:key];
NSLog(@"key = %@ : value = %@", key, value);
}
NSLog(@"----------------------------");
// 高效
for (id key in mutableDic2) {
id value = [mutableDic2 objectForKey:key];
NSLog(@"%@ : %@", key, value);
}
// 用枚举类型枚举
NSEnumerator *enumerator = [mutableDic2 keyEnumerator];
id key;
while (key = [enumerator nextObject]) {
NSLog(@"%@ ---> %@", key, [mutableDic2 objectForKey:key]);
}
五、NSSet和NSMutableSet类
//____________________NSSet________________________
// 集合的创建
NSSet *set1 = [NSSet setWithObjects:@"1", @"2", nil];
NSLog(@"set1 : %@", set1);
NSSet *set2 = [[NSSet alloc] initWithObjects:@"3", @"4", @"2", nil];
NSLog(@"set2 : %@", set2);
NSArray *array = [NSArray arrayWithObjects:@"8", @"9", nil];
NSSet *set3 = [NSSet setWithArray:array];
NSLog(@"set3 : %@", set3);
NSSet *set4 = [NSSet setWithSet:set1];
NSLog(@"set4 : %@", set4);
//取集合的元素个数
int count = [set2 count];
NSLog(@"count : %d", count);
// 将集合入一个数组中
NSArray *array2 = [set2 allObjects];
NSLog(@"array2 : %@", array2);
// 取任意一个元素
id object = [set2 anyObject];
NSLog(@"object : %@", object);
// 是否包含某个对象
BOOL isContain = [set2 containsObject:@"2"];
NSLog(@"isContain : %d", isContain);
// 判断两个set是否存在交集
BOOL isInter = [set1 intersectsSet:set2];
NSLog(@"isInter : %d", isInter);
// 判断两个集合中的元素是否完全相同
BOOL isEqual = [set1 isEqualToSet:set2];
NSLog(@"isEqual : %d", isEqual);
// 判断一个集合是否是另一个集合的子集
BOOL isSub = [set1 isSubsetOfSet:set2];
NSLog(@"isSub : %d", isSub);
// 通过已有的两个集合创建一个新的集合
NSSet *set7 = [NSSet setWithObjects:@"2", nil];
NSSet *set8 = [NSSet setWithObjects:@"3", nil];
NSSet *set9 = [set7 setByAddingObjectsFromSet:set8];
NSLog(@"set9 : %@", set9);
//
// NSMutableSet 继承自NSSet
// 创建可变集合
NSMutableSet *mutableSet1 = [NSMutableSet setWithObjects:@"2", @"1", nil];
NSMutableSet *mutableSet2 = [NSMutableSet setWithObjects:@"3", @"1", nil];
// 集合1减去集合2
[mutableSet1 minusSet:mutableSet2];
NSLog(@"mutableSet1 : %@", mutableSet1);
// 集合2和集合1的交集
[mutableSet1 intersectSet:mutableSet2];
NSLog(@"mutableSet1 : %@", mutableSet1);
// 并集
[mutableSet1 unionSet:mutableSet2];
NSLog(@"mutableSet1 : %@", mutableSet1);
// 将集合一设置为集合二的内容
[mutableSet1 setSet:mutableSet2];
NSLog(@"mutableSet1 : %@", mutableSet1);
// 删除指定的对象
[mutableSet1 removeObject:@"1"];
NSLog(@"mutableSet1 : %@", mutableSet1);