zoukankan      html  css  js  c++  java
  • Object-C—集合

    Obejct-C中包含了三种集合,分别是:数组、字典和集(set)。

     
        数组和C语言中的数组相似,但是OC中的数组只能存储对象,不能存储基本数据类型,如int、float、enum、struct等,也不能存储nil。它也提供了编制好的索引对象,可以通过制定索引找到要查看的对象。包含可变数组(NSMutableArray)和不可变数组(NSArray)。
        字典存放的是“键值对”,即key-value,可以通过键值查找到字典中保存的对象。
        集保存的也是对象,集和字典中保存的对象都是无序的,但是字典可以通过key进行虚拟排序。
        集和字典中不能存在相同的对象,如果对象一致,且是开始定义时写入的,则后面的对象无法写入,如果是后来添加的,则会把原来相同的对象顶替。
     
        基本用法:
    •     数组
        初始化不可变数组:    
    NSArray *array = [[NSArray alloc] initWithObjects:@"one",@"Two",@"Three",nil];此数组只有三个对象,one,two,three,最后的nil可以看出结束符,并不会存入数组中。
        NSLog(@"array count = %d",[array count]);  //打印数组中对象个数
        [array objectAtIndex:2]    //或许索引2处的对象
        初始化可变数组:
    NSMutableArray *Mutablearray = [NSMutableArray arrayWithCapacity:0];    //设置可变数组初始长度为0;
     
        从一个数组拷贝到另一个数组
        Mutablearray = [NSMutableArray arrayWithArray:array];   //将array的对象拷贝到Mutablearray中
     
        在可变数组末尾添加对象
        [Mutablearray addObject:@"Four"];
     
        快速枚举:
        OC中提供了快速又集中的访问遍历数组、字典、集的方法,称为快速枚举
    如,现在array数组中存在的是字符串的对象,所以快速枚举如下:
    for(NSString *str in array)
    {
          NSLog(@"array = %@",str);    //可以一一输出数组array中的对象
    }
        
        从字符串分割到数组- componentsSeparatedByString:
        NSString *string = [NSString alloc] initWithString:@"One,Two,Three,Four"];

        NSLog(@"string:%@",string);    

        NSArray *array = [string componentsSeparatedByString:@","];

        NSLog(@"array:%@",array);

        [string release];

        //从数组合并元素到字符串- componentsJoinedByString:

        NSArray *array = [NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];

        NSString *string = [array componentsJoinedByString:@","];

        NSLog(@"string:%@",string);

        删除可变数组指定索引处的对象:

        [Mutablearray removeObjectAtIndex:1];    

        
     
    •     创建字典
        NSDictionary *dictionary = [NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil];

        NSString *string = [dictionary objectForKey:@"One"];

        NSLog(@"string:%@",string);

        NSLog(@"dictionary:%@",dictionary);

        [dictionary release];

        创建可变字典:

        //创建

        NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

        //添加字典

        [dictionary setObject:@"One" forKey:@"1"];

        [dictionary setObject:@"Two" forKey:@"2"];

        [dictionary setObject:@"Three" forKey:@"3"];

        [dictionary setObject:@"Four" forKey:@"4"];

        NSLog(@"dictionary:%@",dictionary);

        //删除指定的字典

        [dictionary removeObjectForKey:@"3"];

        NSLog(@"dictionary:%@",dictionary);

    •     集合NSSet

        //NSSet中不能存在重复的对象

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

       NSMutableSet *set2 = [[NSMutableSet alloc] initWithObjects:@"1",@"5",@"6", nil]; 

        [set1 unionSet:set2];   //取并集1,2,3,5,6

        [set1 intersectSet:set2];  //取交集1

        [set1 minusSet:set2];    //取差集2,3,5,6

    相关文章:

    http://wenku.baidu.com/link?url=3wlXip1sYdUBWfXjFi58bc_MJChNoMAgA00ooevx8RQCForbdQMxHwJ43DQAAMfL_O7vgR0r1034DWLVWRGCpNjUbigqi1yM-RLEYO2R7fS

  • 相关阅读:
    024.Kubernetes掌握Pod-部署MongoDB
    023.Kubernetes掌握Pod-Pod扩容和缩容
    附010.Kubernetes永久存储之GlusterFS超融合部署
    附009.Kubernetes永久存储之GlusterFS独立部署
    022.Kubernetes掌握Pod-Pod升级和回滚
    021.Kubernetes掌握Pod-Pod调度策略
    020.Kubernetes掌握Pod-Pod基础使用
    018.Kubernetes二进制集群插件metrics-dashboard
    016.Kubernetes二进制集群插件coredns
    .NET Core 3.0之深入源码理解ObjectPool(二)
  • 原文地址:https://www.cnblogs.com/langtianya/p/3913793.html
Copyright © 2011-2022 走看看