GeekBand-OC中的集合类型

NSArray和NSMutableArray

  • 数组是一个有序的元素序列,支持随机存取。索引从0开始,索引越界会抛出运行时异常。
  • NSArray被定义为Class,是引用类型,拷贝时具有引用语义。
  • NSArray的元素必须是对象,即NSObject或其子类:
    1.如为基本数值类型,必须用NSNumber封装为对象类型。
    2.如为C语言结构类型,必须用NSValue封装为对象类型。
    3.数组元素可以是不同对象类型,可能会有类型不安全。
  • NSArray具有常量性:长度和指针都不能更改,但是指针指向的对象内部是可以更改的。

  • NSMutableArray支持更改数组长度和元素。是NSArray的子类。
  • 提前预估好capacity有利于提高效率
  • 尽量避免使用insertObject:<#(nonnull id)#> atIndex:<#(NSUInteger)#>和removeObjectAtIndex:<#(NSUInteger)#>等操作,涉及大量内存操作。
  • 初始化方式
    //工厂方式
    NSArray *array1=[NSArray arrayWithObjects:@"Shanghai",@"Beijing",@"New York",@"Paris", nil];
    //初始化器
    NSArray *array2=[[NSArray alloc] initWithObjects:@"Shanghai",@"Beijing",@"New York",@"Paris", nil];
    //字面常量方式
    NSArray *array3=@[@"Shanghai",@"Beijing",@"New York",@"Paris"];

  • 遍历(由快到慢)
    //快速枚举
    for ( NSObject* obj in array)
    {
    //do something...
    obj
    }

      //迭代器模式
      NSEnumerator *enumerator = [array objectEnumerator];
      NSObject* item;
      while (item = [enumerator nextObject])
      {
          //do something
      }
    
      //for循环
      for (int i=0; i

NSSet和NSMutableSet

  • NSSet是一个无序集合,其储存的对象不能重复。
  • NSSet被定义为Class,是引用类型,拷贝时具有引用语义。

  • NSSet为常量集合长度和指针不能修改,NSMutableSet为可变集合

  • code
    NSSet *set1 =[NSSet setWithObjects:@"Shanghai",@"Beijing",@"New York",@"Paris", nil];
    NSMutableSet *set2 =[NSMutableSet setWithObjects:@"Shanghai",@"Beijing",@"New York",@"Paris", nil];

      [set2 addObject:@"London"];
      [set2 addObject:@"Paris"];
      [set2 removeObject:@"Beijing"];
    
      if([set2 containsObject:@"Shanghai"])
      {
          NSLog(@"set2 contains Shanghai");
      }
    
      for(NSString* item in set2)
      {
          NSLog(@"%@", item);
      }
    

NSDictionary和NSMutableDictionary

  • NSDictionary是一个存储key-value的无序集合,其中key值不可重复,value不限制。
  • NSDictionary被定义为Class,是引用类型,拷贝时具有引用语义。

  • NSDictionary为常量集合长度和指针不能修改,NSMutableDictionary为可变集合

  • code
    BLNPoint *p1=[[BLNPoint alloc] initWithX:10 WithY:20];
    BLNPoint *p2=[[BLNPoint alloc] initWithX:20 WithY:40];
    BLNPoint *p3=[[BLNPoint alloc] initWithX:30 WithY:60];
    BLNPoint *p4=[[BLNPoint alloc] initWithX:40 WithY:80];
    BLNPoint *p5=[[BLNPoint alloc] initWithX:50 WithY:100];

      NSDictionary *dictionary1 = @{@"Shanghai" : p1, @"Beijing" : p2,@"New York" : p3,@"Paris" : p4};
      NSMutableDictionary *dictionary2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:p1,@"Shanghai",p2,@"Beijing",p3,@"New York",p4,@"Paris",nil];
      NSLog(@"dictionary1 count: %lu", dictionary1.count);
      NSLog(@"dictionary2 count: %lu", dictionary2.count);
      BLNPoint* result1=[dictionary1 objectForKey:@"Beijing"];
      BLNPoint* result2=dictionary1[@"Shanghai"];
      NSLog(@"%@", result1);
      NSLog(@"%@", result2);
      for(NSString* key in dictionary1)
      {
          id object=dictionary1[key];
          NSLog(@"key:%@, object:%@", key, object);
      }
    
      [dictionary2 setObject:p5 forKey:@"London"];
      [dictionary2 removeObjectForKey:@"Shanghai"];
      NSLog(@"dictionary2: %@", dictionary2);

你可能感兴趣的:(GeekBand-OC中的集合类型)