NSDictionary

字典创建:

NSDictionary *dict=[NSDictionary dictionaryWithObject:@“value1”,@“key1”];//一组键值对
NSDictionary *dict=[NADictionary dictionaryWithObjectAndKeys:@“value1”,@“key1”,@“value2”,@“key2”];//多组键值对
NSDictionary *dict=@{@“kay1”:@“value1”,@“kay2”:@“value2”};//快速创建字典
根据键获取值
for(NSSTring *key in dict4){
NSLog(@“key=%@,value=%@“,key,[dict4 objectForKey:key];
}
[dict4 enumerateKeysAndObjectsUsingBlock:^(id key,id obj,BOOL *stop){
NSLog(@“%@—>%@”key,obj);
 }]
//讲字典保存到文件中
BOOL a=[dict writeToFile:@“/user/../DeskTop/dict.plist” atomically:YES];
NSDictionary b=[NSDIctionary dictionaryWithContentsOfFile:@“/user/../DeskTop/dict.plist”
 ];

OC字典NSDictionary

 
三种集合类

NSSArray 用于对象有序集合(NSObject对象)
NSSet 用于对象的无序集合
NSDictionary 永固键值映射
以上三种集合类是不可变的(一旦初始化, 就不能改变)

以下是对应的三种可变集合类(者三种可变集合类是对应上面三种集合类的子类)
NSMutableArray
NSMutableSet
NSMutableDictionary

1. 字典的定义

字典用于保存具有映射关系(Key - value对)数据的集合,
对于 "name : 张三" 来讲, key就是"name" , key对应的value值就是"张三", 一个key-value对认为是一个条(Entry), 字典是存储key-value对的容器! 

  • 字典的特点 
    • 与数组不同, 字典靠key存取元素;
    • key值不能重复; value必须是对象;
    • 键值对在字典中是无序存储的
  • NSDictionary 不可变字典
    • 创建字典
          NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
          @"male", @"sex",
          @"20", @"age",
          @"Tom", @"name",
          @"run",@"hobby", nil ];`
    • 获取所有的key值和所有的Value值
      [dic allKeys];
      [dic allValues];
    • 根据key值获取Value值
      [dic valueForkey:@"name"];
      NSLog(@"%@", [dic valueForKey:@"name"])
    • 遍历字典
      for-in 循环 快速遍历, 根据存在的对象类型输出
        for (NSString *key in dic) {
        NSLog(@"%@", key) // key为NSString可I型, 所以能遍历一边!
        }
  • NSMutableDictionary

    • 初始化字典
      NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
      @"male", @"sex", 
      @"20", @"age",
      @"tom", @"name",
      @"run", @"hobby"];
    • 向字典中添加字典: //
      NSDictionary *dict4=[NSDictionary dictionaryWithObject:@"v6"forKey:@"k6"];

    [mutableDic addEntriesFromDictionary:dict4];
    表示 把dict4字典中的对象和key值添加到了mutableDic中

    • 向字典中添加value 和 key
      [mutableDic setValue:@"object" forKey:@"key"];
    • 创建空的字典 , 并设置新的一个字典
      NSMutableDictionary *mutableDict2 = [NSMutableDictionary dictionary];
      `[mutableDict2 setDictionary:mutableDic];
    • 删除指定key的value值
      [mutableDict2 removeObjectForKey:@"k4"];
    • 删除移除key集合的value值
      NSArray *arrayKeys=[NSArray arrayWithObjects:@"k1",@"k2",@"k3",nil];
      [mutableDict2 removeObjectsForKeys:arrayKeys];
    • 删除字典中的所有value值
      [mutableDict2 removeAllObjects]; // 删除所有value值
2.集合

不可变集合:
Foundation框架中, 提供了NSSet类, 它是一组单值对象的集合, 且NSSet实例中元素是无序的, 用一个对象只能保存一个, 并且它也分为可变和不可变 

  • 创建集合
    NSSet *set1 = [[NSSet alloc] initWithObjects:@"one", @"two", nil];
  • 通过数组构建集合
    NSArray *array = [NSArray aarayWithObjects:@"one", @"two", nil];
    NSSet *set = [NSSet setWithArray: array1];
  • 通过已有集合构建集合
    NSSet *set3 = [NSSet setWithSet : set2];
  • 集合对象数量
    NSInteger *count = [set3 count]; NSLog(@"%ld", count);

  • 返回集合中任意元素
    NSString *str = [set3 anyObject]; // 注意返回的集合中的任意值, 随机出现

  • 给集合中添加新元素
    NSSet *set1 = [NSSet setWithObject: @"one", nil];
    NSSet *appSet = [set1 setByAddingObject:@"two"]; NSLog(@"%@", appSet);

可变集合

  • 初始化
    NSMutableSet *mutableSet1 =[NSMutableSet Set];
    NSMutableSet *mutableSet2 = [NSMutableSet setWithObjects:@"1", @"2", nil];
  • 从集合中去除相同的元素
    [mutableSet1 minusSet: mutableSet2];
    从mutableSet2去除mutableSet3中相同的!

  • 得到两个集合的公共元素
    [mutableSet1 intersectSet:mutableSet2];
    mSet1 只留下与mSet2相同的元素

  • 合并两个集合的元素
    [mutableSet unionSet: mutableSet3];

posted @ 2016-11-27 17:28  瓜皮程序员  阅读(145)  评论(0编辑  收藏  举报