天天看點

Object-C之集合(NSSet,NSMutableSet)

#import "ViewController.h"
#import "Student.h"

@interface ViewController (){
    
}
@end

@implementation ViewController

//程式啟動界面顯示之前會調用這個方法
//是以将文法代碼添加在這裡
- (void)viewDidLoad {
    [super viewDidLoad];
    //集合 類似Java語言中的Set
    
    //=========不變集合 NSSet ==========
    NSSet *set=[NSSet setWithObjects:@"jack",@"47",[NSNumber numberWithInteger:99],nil];
    
    //集合大小
    [set count];
    
    //檢查是否包含
    NSString *str=@"關羽";
    if([set containsObject:str]){
        NSLog(@"集合中包含 %@這個對象", str);
    }
    
    //疊代周遊
    NSEnumerator *ennumerator=[set objectEnumerator];
    NSObject *obj=[ennumerator nextObject];
    while (obj!=nil) {
        NSLog(@"疊代器周遊集合中的資料: %@",obj);
        obj = [ennumerator nextObject];
    }
    
    //快速周遊
    for (NSObject *obj in set) {
        NSLog(@"快速枚舉周遊集合中的資料: %@",obj);
    }
    
    //=========可變集合 NSMutableSet ==========
    NSMutableSet *mutableSet=[NSMutableSet setWithCapacity:10];
    
    //添加對象
    [mutableSet addObject:@"AAA"];
    [mutableSet addObject:@"BBB"];
    
    //添加資料在删除
    NSString *string = @"删除我";
    [mutableSet addObject:string];
    //删除它
    [mutableSet removeObject:string];
    
    //快速周遊
    for (NSObject *object in set) {
        NSLog(@"快速枚舉周遊集合中的資料: %@",object);
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end