OC学习之道:OC对象,字典,集合的使用方法NSDictionary,NSMutableDictionary, NSSet,NSMutableSet,NSCountedSet
介绍一个NSValue.它是NSNumber的父类,可以存放结构体等一些对象
int main(int argc, const char * argv[])
{
#pragma mark---NSDictionary不可变字典
//1,初始化,一对
NSDictionary *dict1 = [NSDictionary dictionaryWithObject:@"numObj1" forKey:@"key1"];
NSLog(@"%@",dict1);
//1.1多对
NSDictionary *direc2 = [NSDictionary dictionaryWithObjectsAndKeys:@"number",@"key2",@"number2",@"key3",nil];
NSLog(@"%@",direc2);
//2.获取字典数量
NSInteger count = [direc2 count];
NSLog(@"%ld",count);
//3.根据key值取相应的value对象
NSString *value6 = [direc2 objectForKey:@"key2"];
NSLog(@"%@",value6);
//4.把字典的key转换成枚举对象用于遍历
NSEnumerator *enumerator = [direc2 keyEnumerator];
//5.获取所有的key集合
NSArray *keys = [direc2 allKeys];
NSLog(@"%@",keys);
//6.获取所有的value集合
NSArray *values = [direc2 allValues];
NSLog(@"%@",values);
#pragma mark---NSMutableDictionary可变字典
//1.初始化
NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"a",@"k1",@"b",@"k2",@"c",@"k4",@"d",@"k5",@"e",@"k6", nil];
NSLog(@"%@",mutableDic);
//2.向字典中添加指定的字典
NSDictionary *dir1 = [NSDictionary dictionaryWithObject:@"f" forKey:@"k7"];
[mutableDic addEntriesFromDictionary:dir1 ];
NSLog(@"%@",mutableDic);
//3.向字典中添加value和key
[mutableDic setValue:@"g" forKey:@"k9"];
NSLog(@"%@",mutableDic);
//4.创建空得字典,然后设置新的一个字典
NSMutableDictionary *mutabDir2 = [NSMutableDictionary dictionary];
[mutabDir2 setDictionary:mutableDic ];
NSLog(@"%@",mutabDir2);
//5.删除指定的key和value值
[mutabDir2 removeObjectForKey:@"k4"];
NSLog(@"%@",mutabDir2);
//6.删除移除key集合的value值//
NSArray *arrayKeys = [NSArray arrayWithObjects:@"k1",@"k2",@"k5", nil];
[mutableDic removeObjectsForKeys:arrayKeys];
NSLog(@"%@",mutableDic);
//7.删除字典中所有的value
[mutabDir2 removeAllObjects];
NSLog(@"%@",mutabDir2);
//枚举遍历,在排序篇会给出介绍
NSDictionary *dic3 = [NSDictionary dictionaryWithObjectsAndKeys:@"tongxing",@"name",@"male",@"sex",@(23),@"age", nil];
NSArray *allKeys = [dic3 allKeys];//先获取所有key值
for (NSInteger i = 0; i<[allKeys count]; i++) {
NSString *obj = [dic3 objectForKey:[allKeys objectAtIndex:i]];//allKeys objectAtIndex:i是取出装有allKeys的一个,然后在调用objectForKey来取出这一个key所对应的value值,并赋值给obj
NSLog(@"%@",obj);
}
//快速枚举
for (id key in dic3) {
NSString *object = [dic3 objectForKey:key];
NSLog(@"%@",object);
}
//
#pragma mark-----迭代器枚举(后面详解)
//
//枚举器
NSEnumerator *enumerator1 = [mutableDic keyEnumerator];
id key =[enumerator1 nextObject];
while (key) {
id object = [mutableDic objectForKey:key];
NSLog(@"%@",object);
key = [enumerator1 nextObject];
}
//
//集合类
#pragma mark----NSSet不可变集合
//1.初始化
NSSet *set1 = [[NSSet alloc]initWithObjects:@"one",@"two", nil];
NSLog(@"%@",set1);
//2.通过数组的构建集合
NSArray *array9 = [NSArray arrayWithObjects:@"one",@"two",@"three", nil];
NSSet *set2 = [NSSet setWithArray:array9];
NSLog(@"%@",set2);
//3.通过已有集合进行构建
NSSet *set3 = [NSSet setWithSet:set2];
NSLog(@"%@",set3);
//4.集合对象的数量
NSUInteger count7 = [set2 count];
NSLog(@"%lu",count7);
//5.返回集合中的所有元素
NSArray *array8 = [set3 allObjects];
NSLog(@"%@",array8);
//6.返回集合中的任意一个元素
NSString *str15 = [set3 anyObject];
NSLog(@"%@",str15);
//7.查询集合中是否包含某一个元素
Boolean result1 = [set3 containsObject:@"two"];
NSLog(@"%d",result1);
//8.查询集合和集合是否有交集
BOOL result8 = [set3 intersectsSet:set1];
NSLog(@"%d",result8);
//9.集合的匹配,判断两个集合中的元素是否完全相同
BOOL result3 = [set1 isEqualToSet:set2];
NSLog(@"%d",result3);
//10.是否是一个集合的子集
BOOL isSub = [set1 isSubsetOfSet:set3];//前者是否是后者的子集
NSLog(@"%d",isSub);
//11.在一个集合中添加一个新元素,返回新的集合
NSSet *set6 = [NSSet setWithObjects:@"one", nil];
NSSet *appSet = [set6 setByAddingObject:@"two"];
NSLog(@"%@",appSet);
//12.在一个集合中添加一个集合
NSSet *appSet1 = [set1 setByAddingObjectsFromSet:set6];
NSLog(@"%@",appSet1);
//13.在一个集合中添加一个数组
NSSet *appSet2 = [set1 setByAddingObjectsFromArray:array1];
NSLog(@"%@",appSet2);
#pragma mark----NSMutableSet可变集合
//1.创建初始化可变集合
NSMutableSet *mutableSet1 = [NSMutableSet set];//空集合
NSMutableSet *mutableSet2 = [NSMutableSet setWithObjects:@"1",@"2", nil];
NSMutableSet *mutableSet3 = [NSMutableSet setWithObjects:@"a",@"2", nil];
//2.从集合中去除相同的元素
// [mutableSet2 minusSet:mutableSet3];//从前面的集合中减去和后面的集合相同的数
// NSLog(@"%@",mutableSet3);
//3,求两个集合的公共元素
// [mutableSet2 intersectSet:mutableSet3];
// NSLog(@"%@",mutableSet2);
//4.合并两个集合
[mutableSet2 unionSet:mutableSet3];//合并完后目标集合在前面
NSLog(@"%@",mutableSet2);
//5.删除指定对象
[mutableSet2 removeObject:@"a"];
NSLog(@"%@",mutableSet2);
#pragma mark----计数集合NSCountedSet
NSCountedSet *countSet = [NSCountedSet setWithObjects:@"aa",@"bb",@"cc",@"dd", nil];
NSLog(@"%@",countSet);
NSCountedSet *cSet = [[NSCountedSet alloc] initWithObjects:@"aa", @"bb", @"cc", @"cc", @"dd", @"cc", @"dd", nil];
NSLog(@"cSet:%@", cSet);
//移除@"cc",
//removeObject: 对于NSCountedSet对象而言,移除只能移除一个对象,而对于NSMutableSet而言,移除会将所有相同的对象全部移除。
[cSet removeObject:@"cc"];
NSLog(@"remove:%@", cSet);
//添加@"ff"
//addObject:对于NSCountedSet对象而言,添加对象会增加对象在集合中出现的次数,而对于NSMutableSet而言,如果添加的对象已经存在于集合中,则不会再添加。
[cSet addObject:@"ff"];
[cSet addObject:@"dd"];
NSLog(@"add:%@", cSet);
//获得某个对象在集合中出现的次数
NSUInteger countInSet = [cSet countForObject:@"dd"];
NSLog(@"count = %lu", countInSet);
#pragma mark---快速遍历(快速枚举)//不能进行增删改
for (NSString *element in cSet) {
NSLog(@"%@", element);
}
@autoreleasepool {
}
return 0;
}