ios学习笔记之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中的对象
}
      
       从字符串分割到数组- componentsSeparatedByStr ing:
       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

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值