OC中NSDictionary(字典)、NSMutableDictionary(可变字典)、NSSet(集合)、NSMutableSet(可变集合)得常用方法

字典用于保存具有映射关系数据的集合

一个key—value对认为是一个条目(entry),字典是存储key—value对的容器

与数组不同,字典靠key存取元素

key不能重复,value必须是对象

键值对在字典中是无序存储的

字典分:不可变字典(NSDictionary)和可变字典(NSMutableDictionary)

不可变字典一旦创建,键值对就不可更改,不可添加,不可删除,仅能读取key或者value

常用方法有:

1、创建字典对象

2、获取所有key值,获取所有value值

3、通过key值查询value

1、常见字典的常用方法

在创建字典对象时需要赋值键值对,但是顺序为:值,键,(值在前键在后的形式)

NSDictionary *dic1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"Duke",@"name",@33,@"age",@"男",@"gender", nil];

NSLog(@"%@",dic1);

NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Duke",@"name",@33,@"age",@"男",@"gender", nil];

NSLog(@"%@",dic2);

NSArray *keys = @[@"name",@"age",@"gender"];

NSArray *values = @[@"Duke",@33,@"male"];

字典的语法糖形式

键值对之间以逗号隔开,键和值之间以冒号隔开

NSDictionary *dic5 = @{@"name" : @"Duke",@"age" : @33,@"gender" : @"male"};

NSLog(@"%@",dic5);

创建字典对象时两个数组元素个数必须一致

NSDictionary *dic3 = [[NSDictionary alloc] initWithObjects:values forKeys:keys];

NSLog(@"%@",dic3);

NSDictionary *dic4 = [NSDictionary dictionaryWithObjects:values forKeys:keys];

NSLog(@"%@",dic4);

通过count方法获取字典中键值对的个数

NSInteger count = [dic4 count];

NSLog(@"%ld",count);

获取字典中所有的键

NSArray *allKeys = [dic4 allKeys];

NSLog(@"%@",allKeys);

获取字典中所有的值组成的值

NSArray *allValues = [dic4 allValues];

NSLog(@"%@",allValues);

通过指定的键获取其在字典中对应的值

id object = [dic4 objectForKey:@"age"];

NSLog(@"%@",object);

for (int i = 0; i < count; i++) {

id key = [allKeys objectAtIndex:i];

根据当前遍历得到的key去获取对应的value

id value = [dic4 objectForKey:key];

NSString *result = [value isKindOfClass:[NSString class]] ? @"YES" : @"NO";

NSLog(@"%@:%@-->%@",key,value,result);

}

NSMutableDictionary可变字典

可变NSMutableDictionary是NSDictionary的子类,拥有所有NSDictionary的方法

常用方法有:

1、创建字典对象

2、添加键值对

3、修改key对应的value

4、删除键值对

5、通过fou循环遍历所有键值对

NSMutableDictionary可变字典对象的创建

NSMutableDictionary *dic6 = [[NSMutableDictionary alloc] initWithDictionary:dic5];

NSLog(@"%@",dic6);

NSMutableDictionary *dic7 = [NSMutableDictionary dictionaryWithDictionary:dic5];

NSLog(@"%@",dic7);

NSMutableDictionary *dic8 = [[NSMutableDictionary alloc] init];

NSLog(@"%@",dic8);

NSMutableDictionary *dic9 = [NSMutableDictionary dictionary];

NSLog(@"%@",dic9);

增加键值对

[dic9 setObject:@"Duke" forKey:@"name"];

[dic9 setObject:@"male" forKey:@"gender"];

[dic9 setObject:@33 forKey:@"age"];

NSLog(@"%@",dic9);

修改已有键对应的值

如果键不存在,则为添加键值对,如果键存在,则为修改已有键对应的值

[dic9 setObject:@34 forKey:@"age"];

NSLog(@"%@",dic9);

根据指定键去删除对应的键值对

[dic9 removeObjectForKey:@"age"];

NSLog(@"%@",dic9);

删除所有的键值对

[dic9 removeAllObjects];

NSLog(@"%@",dic9);

OC中的集合(NSSet)与数学中的集合一样,集合中的元素具有唯一性、存储单元的元素是无序的、存储元素必须是对象类型

OC中用Set表示集合,分为NSSet和NSMutableSet

NSSet对象的创建 无序性,互异性

NSSet的常用方法

1、创建集合对象

2、获取元素个数

3、获取集合中的某个元素

4、判断集合中是否包含某个对象

创建集合对象

NSSet *set1 = [[NSSet alloc] initWithObjects:@"1",@"2",@"2",@"3", nil];

NSLog(@"%@",set1);

NSSet *set2 = [NSSet setWithObjects:@"3",@"2",@"1", nil];

NSLog(@"%@",set2);

用数组对象来创建集合对象

可以通过这种方法过滤掉数组中重复的元素对象

NSArray *array = @[@1,@2,@3,@2,@3];

NSSet *set3 = [[NSSet alloc] initWithArray:array];

NSSet *set4 = [NSSet setWithArray:array];

NSLog(@"%@",set3);

NSLog(@"%@",set4);

获取集合中对象的个数

NSInteger count2 = [set4 count];

NSLog(@"%ld",count2);

获取集合中的对象

id object1 = [set4 anyObject];

NSLog(@"%@",object1);

判断一个给定的对象是否包含在指定的集合中

NSString *result1 = [set4 containsObject:@3] ? @"YES" : @"NO";

NSLog(@"%@is contained in set %@",@3,result1);

NSMutableSet的常用方法

1、创建集合对象

2、添加元素

3、删除元素

创建NSMutableset对象

NSMutableSet *mutableSet1 = [[NSMutableSet alloc] init];

NSLog(@"%@",mutableSet1);

NSMutableSet *mutableSet2 = [NSMutableSet set];

NSLog(@"%@",mutableSet2);

通过不可变对象创建

NSMutableSet *mutableSet3 = [[NSMutableSet alloc] initWithSet:set1];

NSLog(@"%@",mutableSet3);

NSMutableSet *mutableSet4 = [NSMutableSet setWithSet:set1];

NSLog(@"%@",mutableSet4);

添加集合元素

[mutableSet4 addObject:@4];

NSLog(@"%@",mutableSet4);

删除单个集合

[mutableSet4 removeObject:@"3"];

NSLog(@"%@",mutableSet4);

删除所有元素

[mutableSet4 removeAllObjects];

NSLog(@"%@",mutableSet4);

NSCountedSet

NSCountedSet是NSMutableString的子类

能记录元素的重复次数

在set的基础上添加了计数功能

NSCountedSet记录添加进去的集合对象出现的次数

NSCountedSet *countedSet = [NSCountedSet set];

[countedSet addObject:@1];

[countedSet addObject:@2];

[countedSet addObject:@2];

NSLog(@"%@",countedSet);

单独获取某个对象在集合中出现过多少次

NSInteger countOfObjec = [countedSet countForObject:@1];

NSLog(@"%ld",countOfObjec);

通过快速枚举来遍历数组元素

NSArray *testArray = @[@1,@2,@3,@4,@5];

for (id object in testArray) {

NSLog(@"%@",object);

}

for (NSNumber *object in testArray) {

NSLog(@"%@",object);

}

快速遍历集合

for (id object in set1) {

NSLog(@"%@",object);

}

快速遍历字典

直接遍历字典直接得到的是字典的每一个键,可以通过遍历得到的键去获取对应的值

for (NSString *key in dic1) {

NSLog(@"dictionary[%@]:%@",key,dic1[key]);

}

//    dic1[key]就相当于[dic1 objectForKey:key]


文章转载自

OC中NSDictionary(字典)、NSMutableDictionary(可变字典)、NSSet(集合)、NSMutableSet(可变集合)得常用方法 - Running2Snail - 博客园

你可能感兴趣的:(OC中NSDictionary(字典)、NSMutableDictionary(可变字典)、NSSet(集合)、NSMutableSet(可变集合)得常用方法)