Collection类

简介

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类)