zoukankan      html  css  js  c++  java
  • OC 字典 集合 数组

    ////  main.m
    
    #import <Foundation/Foundation.h>
    
    int main(int argc, const char * argv[]) {
    
    #pragma mark--字典 NSDictionary----
        
        //字典用来存储具有一一对应关系的数据,本质上是一个大容器
        
        //创建对象:
        //1.首先使用初始化方法创建对象:
       //字典中存储的数据必需要是key-value成对存在,并且value在前,key在后,字典特点是:无序性.
    //    
    //    
    //    NSDictionary *dict=[[NSDictionary alloc]initWithObjectsAndKeys:@"duck",@"鸭子",@"猫",@"cat",@"狗",@"dog", nil];
    //    NSLog(@"%@",dict);
    //    
    //    
    //    //2.便利构造器
    //    //wangwu   : value   --- value可以重复
    //    //name     :   key     --key是不能重复的
    //    NSDictionary *dict1=[NSDictionary dictionaryWithObjectsAndKeys:@"wangwu",@"name",@"nan",@"sex", nil];
    //    
    //    NSLog(@"%@",dict1);
    //    
    //    //讲数组充当我们字典中的元素;
    //    //我们一般写的时候先写的是value,后写的是key,而读取和打印的时候,都是 key,value;
    //    NSDictionary *dict2=[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"zhaosi",@"男",@"吃糖豆", nil] forKeys:[NSArray arrayWithObjects: @"name",@"sex",@"hobby",nil]];
    //    
    //    NSLog(@"%@",dict2);
    //    
    //    //获取字典当中对象的所有key:allKeys
    //    NSLog(@"allkeys=%@",[dict2 allKeys]);
    //    //获取字典当中对象的所有value:allValues
    //    NSLog(@"value=%@",[dict2 allValues]);
    //    //字典是通过我们的key找到与之相对应的value
    //    NSLog(@"name=%@",[dict2 objectForKey:@"sex"]);
    //    
    //    //语法糖:字面量(也就是使用最简单快捷的方式去创建),与之对应的是语法盐(也就是使用复杂方式创建出来).
    //    //字典:里面元素存储是无序的,要想取值只能通过key去取对应的value;
    //    NSDictionary *dict3=@{@"name":@"zhangsan",@"gender":@"nan",@"age":@"18",@"class":@"sd1232"};
    //    NSLog(@"%@",dict3);
    //    
    //    /*
    //     获取字典当中元素个数
    //     */
    //    NSUInteger count=[dict3 count];
    //    NSLog(@"count=%lu",count);
    //    
        
    #pragma mark--    NSMutableDictionary   可变字典----
        //首先创建一个可变字典
        
        
    //    NSMutableDictionary *mutabledic=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"lisi",@"name",[NSNumber numberWithInt:10],@"age", nil];
    //    NSLog(@"%@",mutabledic);
    //    
    //    NSMutableDictionary *mdic=[[NSMutableDictionary alloc]initWithCapacity:0];
    //    //往字典中添加元素
    //    [mdic setObject:@"huluwa" forKey:@"name"];
    //    [mdic setObject:@"xiaojingang " forKey:@"hobby"];
    //    NSLog(@"%@",mdic);
    //    
    //    //修改key对应的value
    //    
    ////setobject: forkey:工作原理:先查找字典当中有没有对应的key如果有则修改key对应的calue,如果没有,则把key-value作为元素添加到字典中
    //    [mdic setObject:@"hulumei" forKey:@"name"];
    //    NSLog(@"%@",mdic);
    //    
    //    [mdic setObject:@"18" forKey:@"age"];
    //    NSLog(@"%@",mdic);
    //    
    //    //移除
    //    //移除一个元素
    //    [mdic removeObjectForKey:@"name"];
    //    NSLog(@"%@",mdic);
    //    //移除所有元素
    //    [mdic removeAllObjects];
    //    NSLog(@"%@",mdic);
    //    
        //字典移除完所有元素之后,字典空间依然存在
        
        
        /*
         使用字典完成:"市,区"数据管理;
         1.使用字典表示一个城市的区名和人数;
         2.使用字典表示一个城市,城市名和所有的市区
         
    //     */
    //    NSDictionary *dic1=[NSDictionary dictionaryWithObjectsAndKeys:@"chaoyang",@"qm",@"31333",@"number", nil];
    //    NSDictionary *dic2=[NSDictionary dictionaryWithObjectsAndKeys:@"haidian",@"qm",@"11111",@"number", nil];
    //    NSDictionary *dic3=[NSDictionary dictionaryWithObjectsAndKeys:@"tongzhou",@"qm",@"23232",@"number", nil];
    //    NSDictionary *dic4=[NSDictionary dictionaryWithObjectsAndKeys:@"tongzhou",@"qm",@"55245",@"number", nil];
    // //将上述定义的字典放到数组当中
    //    NSArray *array=[NSArray arrayWithObjects:dic1,dic2,dic3,dic4, nil];
    //    
    ////    NSLog(@"%@",array);
    //    
    //    //定义个字典
    //    NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:@"beijing",@"cityname",array,@"sqm", nil];
    //    NSLog(@"%@",dict);
        
        
        /*
         
         NSArray数据写入文件中
        
        NSArray *array=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
        //将我们数组当中的元素写入数据文件当中(plist)
        if ([array writeToFile:@"/Users/lanou3g/Desktop/arr.plist" atomically:YES]) {
            NSLog(@"写入plist文件成功");
        }else{
            NSLog(@"没有写入!!");
        }
        
        
        //创建一个数组读取plist文件当中的数据
        NSArray *arr1=[NSArray  arrayWithContentsOfFile:@"/Users/lanou3g/Desktop/arr.plist"];
        
        NSLog(@"%@",arr1);
        
        
        
        
        //创建一个字典数据
        NSDictionary *dictionary=@{@"name":@"shuguang",@"sex":@"nan",@"age":@"18"};
        
        //将字典中的数据写入文件中
        if ([dictionary writeToFile:@"/Users/lanou3g/Desktop/dict.plist" atomically:YES]) {
            NSLog(@"数据写入成功!!");
            
        }else {
        
        
            NSLog(@"数据写入失败");
        }
        //从文件中读取数据
        NSDictionary *dict=[NSDictionary dictionaryWithContentsOfFile:@"/Users/lanou3g/Desktop/dict.plist" ];
        NSLog(@"%@",dict);
        
        
        //将两个字典放入plist文件中
        NSDictionary *dict1=@{@"sg":@"shuguang",@"gy":@"guoya",@"dl":@"deliang",@"jc":@"jiechen"};
        NSDictionary *dict2=@{@"a":@"ab",@"b":@"bc",@"c":@"cd",@"d":@"de"};
        
        //将两个字典放入一个数组当中
        NSArray *array1=@[dict1,dict2];
        
        //讲述组写入到文件当中
        [array1 writeToFile:@"/Users/lanou3g/Desktop/dict.plist" atomically:YES];
        NSLog(@"%@",array1);
        
        */
    #pragma mark --集合 NSSet--
        /*
       //集合特点:存储是无序的,互异的,本质上也是一个大容器,内部元素具有唯一性,不可以重复
        
        //不可变
    //    NSSet *set=[[NSSet alloc]initWithObjects:@"aa",@"bb",@"cc",@"dd",@"aa",@"bb",@"cc", nil];
    //    NSLog(@"%@",set);
    //    NSLog(@"%ld",[set count]);
    //    
         */
    //    
    //     NSNumber *num1=[NSNumber  numberWithInt:1];
    //     NSNumber *num2=[NSNumber  numberWithInt:2];
    //     NSNumber *num3=[NSNumber  numberWithInt:3];
    //     NSNumber *num4=[NSNumber  numberWithInt:4];
    //     NSNumber *num5=[NSNumber  numberWithInt:5];
    //   NSSet *set1=[NSSet setWithObjects:num1,num2,num3,num4,num5, nil];
        
        //member :判断对象是不是集合的成员,如果不在集合当中就返回空,如果在就直接返回该对象.
        
       // NSLog(@"%@",[set1 member:num5]);
        
        //containsObject 判断集合中是否包含一个元素,如果该对象在集合中那么就返回1,如果不在就返回0
        //NSLog(@"%d",[set1 containsObject:num2]);
        //获取集合当中的元素
        //anyObject它是获取集合中的元素,而集合当中元素存储是无序的,使用anyObject一般获取的是第一个元素的值,但是系统会获取一个较为安全的值,所以使用它是不确实的
    //    NSLog(@"%@",[set1 anyObject]);
    //    NSLog(@"%@",[set1 anyObject]);
        
        
    #pragma mark --可变集合 NSMutablieset--
       /*
        NSMutableSet *mset=[[NSMutableSet alloc]initWithCapacity:0];
        //添加元素
        [mset addObject:@"frank"];
        [mset addObject:@"duck"];
        [mset addObject:@"jack"];
        [mset addObject:@"lanou"];
        NSLog(@"%@",mset);
        //移除元素
        [mset removeObject:@"duck"];
        NSLog(@"%@",mset);
        
        
        //遍历集合中的元素
        
        for (NSString *str in mset) {
            NSLog(@"%@",str);
        }
        
        */
        
    #pragma mark--数组,字典,集合区别--
        
        //相同点:都是oc中的容器类,可以存储多个元素,但是必须是对象,不限制对象类型
        
        
        /*
        //不同点:
        1.使用场景:
         数组:用来管理一组有序的元素集合
         字典:用来管理一组具有一一对应关系的集合(键值对)
         集合:用来管理一组无序具有互异性的元素的集合
         2.特点:
         
         数组:有序,元素可以重复
         字典:无序,kay-value是字典中的一个元素,并且key是唯一的一个key只能对应一个value,但是一个value可以对应多个key
         集合:无序的,元素具有互异性
         
         3.取值方式:
         数组:可以通过索引获取对应的元素
         字典:通过key获取对应的value
         集合:anyObject----一般获取的都是第一个元素的值
         
         4.for in 快速遍历(快速枚举):当数组字典集合 进行快速遍历时集合不能发生更改,不能添加元素,也不能删除元素.
         数组:遍历出数组中每个元素
         字典:遍历出当中的key,通过key获取value
         集合:遍历出集合当中的每个元素
         
         5.创建对象:
         (1).初始化方法:
         数组:initWithObjects:
         
         字典:initWithObjectsAndKes:
         
         集合:initWithObjects:
         
         (2).遍历构造器:
         
         数组:arrayWithObjects
         
         字典:dictionaryWithObjectsAndKeys:
         
         集合:setWithObjects:
         
         
         
        
        
        */
    //    
    //    NSMutableArray *array=[NSMutableArray arrayWithObjects:[NSNumber numberWithInt:12],[NSNumber numberWithInt:16],[NSNumber numberWithInt:1],[NSNumber numberWithInt:89],[NSNumber numberWithInt:43],[NSNumber numberWithInt:21],[NSNumber numberWithInt:4],[NSNumber numberWithInt:6], nil];
        
    //    for (int i=0; i<[array count]-1; i++) {
    //        for (int j=0; j<[array count]-1-i; j++) {
    //            if ([array[j] intValue]>[array[j+1] intValue]) {
    //                NSNumber *temp=array[j];
    //                array[j]=array[j+1];
    //                array[j+1]=temp;
    //                
    //            }
    //        }
    //    }NSLog(@"%@",array);
    //    
        
    //    for (int i=0; i<[array count]-1; i++) {
    //        for (int j=0; j<[array count]-1-i; j++) {
    //            if ([array objectAtIndex:j]>[array objectAtIndex:j+1]) {
    //                [array exchangeObjectAtIndex:j withObjectAtIndex:j+1];
    //            }
    //    
    //        }
    //    }
    //    for (NSNumber *num in array) {
    //        NSLog(@"%@",num);
    //    }
        
        //字面量:语法糖
        
        //在oc中使用字面量前面有@表示是在创建对象,没有@表示的是取值的操作
        
    //    NSNumber *num1=[NSNumber numberWithInt:10];
    //    //使用字面量创建
    //    NSNumber *num2=@12;
    //    NSLog(@"%@",num2);
    //    
    //    //使用字面量创建数组
    //    NSArray *arr=@[num1,num2 ];
    //    NSLog(@"arr=%@",arr);
    //    NSArray *arr1=@[@12,@13,@14];
    //    NSLog(@"%@",arr1);
    //    
    //    //使用字面量创建字典
    //    
    //    NSDictionary *dic=@{@"name":@"zhangsan",@"age":@"3"};
    //    NSLog(@"%@",dic);
    //    
    //    NSLog(@"%@",dic[@"age"]);
    //    
        
        //int/**/b=20;  //程序编译的时候默认将注释变为空格
        
       // int/**/t c=20;
       
        return 0;
    }
  • 相关阅读:
    并查集
    贪心
    分治
    二分
    操作系统基础知识--《操作系统原理(第4版)(普通高等教育"十一五"国家级规划教材)》读书笔记
    《自控力》读书记录
    计算机组成原理基础知识--《计算机组成原理与汇编语言程序设计(第4版)(高等学校规划教材)》读书笔记
    ansible自动部署模板文件中列表长度判断
    《股市稳赚》摘录
    yarn学习小结
  • 原文地址:https://www.cnblogs.com/huyibo/p/5475332.html
Copyright © 2011-2022 走看看