簡(jiǎn)介
Collection類的實(shí)例用于保存指向其他對(duì)象的指針。(NSArray類及其子類NSMutableArray/NSSetNSMutableSet,NSDictionary/NSMutableDictionary)
NSSet/NSMutableSet
- NSSet對(duì)象所包含的“內(nèi)容”是無序的,而且在一個(gè)NSSet對(duì)象中,某個(gè)特定的對(duì)象只能出現(xiàn)一次。
- NSSet最大用處是檢查某個(gè)對(duì)象是否存在。
- 可以對(duì)指針進(jìn)行修改和不可對(duì)指針進(jìn)行修改兩類:NSSet對(duì)象是不能對(duì)指針進(jìn)行修改的——對(duì)象創(chuàng)建好后就不能對(duì)其中的指針進(jìn)行添加或刪除等操作。NSMutableSet是NSSet的子類,能夠?qū)ζ渲械闹羔樳M(jìn)行添加或刪除等操作。
部分實(shí)現(xiàn)代碼
//NSMutableSet對(duì)象聲明
eOne.assets = [[NSMutableSet alloc]init];
//需要添加到NSMutableSet對(duì)象的BNRAsset自定義對(duì)象屬性參數(shù)設(shè)置
BNRAsset *assetsOne = [[BNRAsset alloc]init];
assetsOne.resaleValue = 51;
assetsOne.label = @"Laptop 3";
assetsOne.holder = eOne;
//添加到NSMutableSet對(duì)象中
[eOne.assets setByAddingObject:assetsOne];
練習(xí)中創(chuàng)建關(guān)系圖的main代碼(省略自定義類)
NSMutableArray *employee = [[NSMutableArray alloc]init];
//創(chuàng)建第一個(gè)員工
BNREmployee *eOne = [[BNREmployee alloc]init];
eOne.heightInMeters = 1.8;
eOne.weightInKilos = 96;
eOne.employeeID = 0;
//創(chuàng)建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];
//創(chuàng)建NSSet對(duì)象并初始化
eOne.assets = [[NSMutableSet alloc]initWithObjects:assetsOne,assetsTwo, nil];
//把BNREmployee對(duì)象eOne添加到數(shù)組employee中
[employee addObject:eOne];
//創(chuàng)建第二個(gè)員工
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對(duì)象eOne.assets中是否存在對(duì)象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++) {
//創(chuàng)建員工
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"];
//創(chuàng)建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];
//創(chuàng)建NSSet對(duì)象并初始化
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;