美文网首页
Collection类

Collection类

作者: 开发者zhang | 来源:发表于2017-07-01 01:52 被阅读0次

简介

Collection类的实例用于保存指向其他对象的指针。(NSArray类及其子类NSMutableArray/NSSetNSMutableSet,NSDictionary/NSMutableDictionary)

NSSet/NSMutableSet

  1. NSSet对象所包含的“内容”是无序的,而且在一个NSSet对象中,某个特定的对象只能出现一次。
  2. NSSet最大用处是检查某个对象是否存在。
  3. 可以对指针进行修改和不可对指针进行修改两类:NSSet对象是不能对指针进行修改的——对象创建好后就不能对其中的指针进行添加或删除等操作。NSMutableSet是NSSet的子类,能够对其中的指针进行添加或删除等操作。

部分实现代码

//NSMutableSet对象声明
eOne.assets = [[NSMutableSet alloc]init];

//需要添加到NSMutableSet对象的BNRAsset自定义对象属性参数设置
BNRAsset *assetsOne = [[BNRAsset alloc]init];
assetsOne.resaleValue = 51;
assetsOne.label = @"Laptop 3";
assetsOne.holder = eOne;

//添加到NSMutableSet对象中
[eOne.assets setByAddingObject:assetsOne];

练习中创建关系图的main代码(省略自定义类)

NSMutableArray *employee = [[NSMutableArray alloc]init];
   
//创建第一个员工
BNREmployee *eOne = [[BNREmployee alloc]init];
eOne.heightInMeters = 1.8;
eOne.weightInKilos = 96;
eOne.employeeID = 0;

//创建BNRAsset
BNRAsset *assetsOne = [[BNRAsset alloc]init];
assetsOne.resaleValue = 51;
assetsOne.label = @"Laptop 3";
assetsOne.holder = eOne;

[eOne.assets setByAddingObject:assetsOne];
   
BNRAsset *assetsTwo = [[BNRAsset alloc]init];
assetsTwo.resaleValue = 119;
assetsTwo.label = @"Laptop 7";
assetsOne.holder = eOne;
   
[eOne.assets setByAddingObject:assetsTwo];
   
//创建NSSet对象并初始化
eOne.assets = [[NSMutableSet alloc]initWithObjects:assetsOne,assetsTwo, nil];

//把BNREmployee对象eOne添加到数组employee中
[employee addObject:eOne];

   
//创建第二个员工
BNREmployee *eTwo = [[BNREmployee alloc]init];
eTwo.heightInMeters = 1.7;
eTwo.weightInKilos = 98;
eTwo.employeeID = 1;
   
BNRAsset *assetsThree = [[BNRAsset alloc]init];
assetsThree.resaleValue = 34;
assetsThree.label = @"Laptop 2";
assetsThree.holder = eTwo;

eTwo.assets = [[NSMutableSet alloc] initWithObjects:assetsThree, nil];

[employee addObject:eTwo];

NSMutableArray *allAssets = [[NSMutableArray alloc] init];
[allAssets addObject:assetsOne];
[allAssets addObject:assetsTwo];
[allAssets addObject:assetsThree];
   
   
//        if ([assetsOne isEqual:assetsTwo]) {
//            NSLog(@"Yes,it is");
//        } else {
//            NSLog(@"NO,it isn't");
//        }
   
NSLog(@"%lu",(unsigned long)[eOne.assets count]);

//判断NSSet对象eOne.assets中是否存在对象assetsOne
if ([eOne.assets containsObject:assetsTwo]) {
  NSLog(@"Yes,it is");
} else {
  NSLog(@"No,it isn't");
}

使用字典(省略自定义类)

//使用字典
NSMutableDictionary *executives = [[NSMutableDictionary alloc]init];
   
NSMutableArray *employee = [[NSMutableArray alloc]init];
   
for (int i = 0; i<2; i++) {
  //创建员工
  BNREmployee *emp = [[BNREmployee alloc]init];
  emp.heightInMeters = 1.8 + i;
  emp.weightInKilos = 90 + i;
  emp.employeeID = i;
  [employee addObject:emp];
  
  if (i == 0) {
      [executives setObject:emp forKey:@"CEO"];
  }
  if (i == 1) {
      [executives setObject:emp forKey:@"CTO"];
  }

}
BNREmployee *eOne = executives[@"CEO"];
   
//创建BNRAsset
BNRAsset *assetsOne = [[BNRAsset alloc]init];
assetsOne.resaleValue = 51;
assetsOne.label = @"Laptop 3";
assetsOne.holder = eOne;
   
[eOne.assets setByAddingObject:assetsOne];
   
BNRAsset *assetsTwo = [[BNRAsset alloc]init];
assetsTwo.resaleValue = 119;
assetsTwo.label = @"Laptop 7";
assetsOne.holder = eOne;
   
[eOne.assets setByAddingObject:assetsTwo];
   
//创建NSSet对象并初始化
eOne.assets = [[NSMutableSet alloc]initWithObjects:assetsOne,assetsTwo, nil];
   
BNREmployee *eTwo = executives[@"CTO"];
BNRAsset *assetsThree = [[BNRAsset alloc]init];
assetsThree.resaleValue = 34;
assetsThree.label = @"Laptop 2";
assetsThree.holder = eTwo;
   
eTwo.assets = [[NSMutableSet alloc] initWithObjects:assetsThree, nil];
   
NSMutableArray *allAssets = [[NSMutableArray alloc] init];
[allAssets addObject:assetsOne];
[allAssets addObject:assetsTwo];
[allAssets addObject:assetsThree];

//输出
NSLog(@"executives: %@",executives);
NSLog(@"@CEO: %@",executives[@"CEO"]);
executives = nil;
   
NSLog(@"Giving up ownership of arrays");
   
allAssets = nil;
employee = nil;

相关文章

  • Collection类

    简介 Collection类的实例用于保存指向其他对象的指针。(NSArray类及其子类NSMutableArra...

  • 我所了解的ArrayList源码

    继承及接口 首先你要知道List类继承了Collection类,而Collection类继承了Iterator类。...

  • java数据结构概述

    一 类继承图 1.1 collection 1.2 map 二 collection 2.1 Collection...

  • 2020-06-28【集合-Collection】

    Collection概述 集合类体系结构 Collection以及常用方法 Collection遍历 练习

  • collections相关的数据结构及API

    1、列举几个Java Collection类库中的常用类 此处应该有Collection类图。 Collectio...

  • 集合篇一

    Collection 分为两类:List和Set。JDK不提供直接继承自Collection的类,JDK提供的类都...

  • 24章:Collection 类

    Collection类 Collection类的实例用于保存指向其他对象的指针。 24.1NSSet/NSMuta...

  • 集合

    常用集合类 单列集合 Collection:Iterable 是单列集合类的根接口 Collection用于存储一...

  • Collection集合方法实例

    Collection 概述 Collection 是所有容器集合的父类接口。Collection 表示一组对象,这...

  • JAVA中的集合框架 List (二)

    Collection接口List接口简介 Collection接口、子接口及其实现类,Collection接口是j...

网友评论

      本文标题:Collection类

      本文链接:https://www.haomeiwen.com/subject/stlwcxtx.html