字典,集合
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 List
NSDictionary *dictionary5 = [NSDictionary dictionaryWithContentsOfFile:@"/Users/lanou3g/Desktop/OC/lesson5-20140425/lesson5-20140425/dictionary.plist"];
NSLog(@"%@", dictionary5);
// 返回字典中键值对的个数
NSLog(@"%ld", [dictionary5 count]);
// 字典取值 获取指定key对应的value
NSString *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创建可变字典 NSMutableDictionary
NSMutableDictionary *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);
posted on 2014-08-01 08:14 woailuoyao 阅读(206) 评论(0) 编辑 收藏 举报