首页 > 代码库 > 字典,集合
字典,集合
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", @"v3", @"k3", @"v5", @"k5", @"v4", @"k4", @"value6", @"key6", @"value7", @"ke7", nil];NSLog(@"%@", dictionary);// 使用类方法创建NSDictionary *dictionary2 = [NSDictionary dictionaryWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", nil];NSLog(@"%@", dictionary2);NSDictionary *dictionary3 = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];NSLog(@"%@", dictionary3);NSArray *keyArray = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil]; // 存放键的数组NSArray *valueArray = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];// 存放值的数组NSDictionary *dictionary4 = [NSDictionary dictionaryWithObjects: keyArray forKeys:valueArray];NSLog(@"%@", dictionary4);// 创建空字典NSDictionary *dic = [NSDictionary dictionary];// 使用一个文件创建字典对象 新建文件步骤:command + N -> Resource -> Property ListNSDictionary *dictionary5 = [NSDictionary dictionaryWithContentsOfFile:@"/Users/lanou3g/Desktop/OC/lesson5-20140425/lesson5-20140425/dictionary.plist"];NSLog(@"%@", dictionary5);// 返回字典中键值对的个数NSLog(@"%ld", [dictionary5 count]);// 字典取值 获取指定key对应的valueNSString *value1 = [dictionary5 objectForKey:@"ly"];NSLog(@"%@", value1);// 返回所有的key数组NSArray *allKeysArray = [dictionary5 allKeys];NSLog(@"%@", allKeysArray);// 返回所有的value数组NSArray *allValuesArray = [dictionary5 allValues];NSLog(@"%@", allValuesArray);// 使用key枚举器(获取所有key)NSEnumerator *enumerator = [dictionary5 keyEnumerator];NSString *str = nil;while (str = [enumerator nextObject]) { NSLog(@"%@", str); }4.2创建可变字典 NSMutableDictionaryNSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", nil];NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"v3", @"k3", @"v4", @"k4", @"v5", @"k5", nil];// 用于整理对象的拼接[dic1 addEntriesFromDictionary:dic2];NSLog(@"%@", dic1);// 删除字典中某个对象[dic1 removeObjectForKey:@"k1"];NSLog(@"%@", dic1);// 删除字典全部对象[dic1 removeAllObjects];NSLog(@"%@", dic1);// 设置字典[dic1 setDictionary:dic2];NSLog(@"%@", dic1);5.1NSSet集合对象 容器类// 1. 使用类方法创建对象NSSet *set1 = [NSSet set]; // 创建一个空的集合对象NSSet *set2 = [NSSet setWithObject:@"abc"];NSSet *set3 = [NSSet setWithObjects:@"abc", @"aaa", @"bbb", nil];NSLog(@"%@", set3);NSArray *array = [NSArray arrayWithObjects:@"a",@"b", @"c", nil];NSSet *set4 = [NSSet setWithArray:array]; // 使用数组创建NSLog(@"%@", set4);NSSet *set5 = [NSSet setWithSet:set4]; // 使用集合创建NSLog(@"%@", set5);// 2.使用实例方法创建NSSet *set6 = [[NSSet alloc] init];NSLog(@"%@", set6);NSSet *set7 = [[NSSet alloc] initWithObjects:@"hello", @"hhaa", @"bbjdh", nil];NSLog(@"%@", set7);NSSet *set8 = [[NSSet alloc] initWithArray:array];NSLog(@"%@", set8);NSSet *set9 = [[NSSet alloc] initWithSet:set7];NSLog(@"%@", set9);// 3.返回几个元素个数NSLog(@"%ld", [set7 count]);// 4.枚举器访问集合元素NSEnumerator *enumerator = [set7 objectEnumerator];NSString *str = nil;while (str = [enumerator nextObject]) { NSLog(@"%@", str); }// 5.判断两个几个是否有交集BOOL ifhasIntersection = [set2 intersectsSet:set3];NSLog(@"%d", ifhasIntersection);// 6.判断两个集合是否相等NSLog(@"%d", [set2 isEqualToSet:set3]);// 7.判断当前集合是否是子集NSLog(@"%d", [set2 isSubsetOfSet:set3]);5.2可变集合 NSMutableSet// 创建指定元素个数的一个集合对象NSMutableSet *mutableSet = [NSMutableSet setWithCapacity:4];[mutableSet addObject:@"aaa"];NSLog(@"%@", mutableSet);// 类方法创建可变集合NSMutableSet *mutableSet1 = [NSMutableSet setWithObjects:@"aaa", @"bbb", @"ccc", nil];NSMutableSet *mutableSet2 = [NSMutableSet setWithObject:@"aaa"];// 添加一个对象到集合[mutableSet2 addObject:@"ddd"];NSLog(@"%@", mutableSet2);// 从集合中删除一个对象[mutableSet2 removeObject:@"ddd"];NSLog(@"%@", mutableSet2);// 把数组对象添加到集合对象中NSArray *arr = [NSArray arrayWithObjects:@"eee", @"fff", nil];[mutableSet1 addObjectsFromArray:arr];NSLog(@"%@", mutableSet1);// 得到两个集合的交集 注意intersectSet和intersectsSet的区别,后者是判断是否有交集的方法, 返回的是bool值[mutableSet1 intersectSet:mutableSet2];NSLog(@"%@", mutableSet1);// 从一个集合中减去另一个集合[mutableSet1 minusSet:mutableSet2];NSLog(@"%@", mutableSet1);// 从一个元素中删除所有元素[mutableSet2 removeAllObjects];NSLog(@"%@", mutableSet2);// 取两个集合的并集[mutableSet1 unionSet:mutableSet2];NSLog(@"%@", mutableSet1);NSLog(@"%@", mutableSet1);// 设置给集合赋值[mutableSet1 setSet:mutableSet2];NSLog(@"%@", mutableSet1);关于数组排序NSArray *array = [NSArrayarrayWithObjects:@"12", @"454", @"7878", @"77122", @"555", @"9", nil];NSLog(@"排序前:%@", array);// 这样排序是按照字符串大小来排序的不是数值排序array = [array sortedArrayUsingSelector:@selector(compare:)];NSLog(@"排序后:%@", array);// 进行数值排序需要用以下的方法array = [array sortedArrayUsingFunction:intSort context:NULL];NSLog(@"排序后:%@", array);以下是intSort 的方法实现:NSInteger intSort(id num1, id num2, void *context){ int v1 = [num1 intValue]; int v2 = [num2 intValue]; if (v1 < v2) returnNSOrderedAscending; else if (v1 > v2) returnNSOrderedDescending; else returnNSOrderedSame; }关于字典与数组/// 字典里可以存数组,数组可以存字典NSArray *arr = [NSArray arrayWithObjects: @"1", @"2", @"3", nil];// 把数组arr放在字典dic里NSDictionary *dic = [NSDictionary dictionaryWithObject:arr forKey:@"array"];NSLog(@"%@", dic);// 把字典dic和数组arr 放在数组arr2里NSArray *arr2 = [NSArray arrayWithObjects:arr, dic, nil];NSLog(@"%@", arr2);
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。