OC-FUNDATION-NSSet&NSMutableSet

1.NSSet

1.1集合,是無序、沒有重複元素的數組
1.2創建方法(3種)

#import <Foundation/Foundation.h>
#import "SHStudent.h"
#import "SHTeacher.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSSet *set1 = [NSSet setWithObjects:@"one",@"two",@"three",@"two", nil];//標準創建方法
        NSLog(@"%@",set1);

        NSSet *set2 = [NSSet setWithSet:set1];//副本創建方法
        NSLog(@"%@",set2);

        NSArray *str = @[@"one",@"two",@"two",@"three"];
        NSSet *set3 = [NSSet setWithArray:str];
        NSLog(@"%@",set3);

        SHStudent *stu1 = [SHStudent studentWithName:@"zhangsan" andAge:18];
        SHStudent *stu2 = [SHStudent studentWithName:@"lisi" andAge:19];
        SHStudent *stu3 = [SHStudent studentWithName:@"wangwu" andAge:20];
        SHStudent *stu4 = [SHStudent studentWithName:@"zhangsan" andAge:18];
        NSSet *set4 = [NSSet setWithObjects:stu1,stu2,stu3,stu4,nil];
        NSLog(@"%@",set4);
        NSLog(@"------------------");
        SHTeacher *t1 = [SHTeacher teacherWithName:@"zhangsan" andCourse:@"math"];
        SHTeacher *t2 = [SHTeacher teacherWithName:@"lisi" andCourse:@"english"];
        SHTeacher *t3 = [SHTeacher teacherWithName:@"wangwu" andCourse:@"history"];
        SHTeacher *t4 = [SHTeacher teacherWithName:@"zhangsan2" andCourse:@"math"];
        NSSet *set5 =[NSSet setWithObjects:t1,t2,t3,t4, nil];
        NSLog(@"%@",set5);
    }
    return 0;
}

1.3自定義類的對象防止重複時需要重寫以下方法:

–1.3.1粗慮-(NSUInteger)hash
–1.3.2細慮-(BOOL)isEqual:(id)object

-(NSUInteger)hash//粗慮
{
    return self.age;//如果沒有整型數據就返回YES
}
-(BOOL)isEqual:(id)object//細慮
{
    if (self == object) {
        return YES;//表示兩個對象重複
    }
    if ([object isMemberOfClass:[SHStudent class]]) {//object是不是student的對象
        SHStudent *s = object;
        if ([self.name isEqualToString:s.name] && self.age == s.age) {
            return YES;
        }
    }
    return NO;//表示沒有重複
}

1.4判斷集合中是否擁有指定的元素

if ([set1 containsObject:@"two"]) {
    NSLog(@"集合set1中包含two元素");
}

1.5判斷兩個集合是否相等

if ([set1 isEqualToSet:set2]) {
    NSLog(@"set1和集合set2相等");
}

1.6判斷某個集合是否爲另一個集合的子集

NSSet *set6 = [NSSet setWithObjects:@"one",@"three", nil];
if ([set6 isSubsetOfSet:set1]) {
    NSLog(@"集合set6是集合set1的子集");
}

1.7將集合轉換成數組

NSArray *array = [set1 allObjects];

1.8遍歷(3種)

for (int i = 0; i < set1.count; i++) {
    NSLog(@"%@",array[i]);//用面向過程的方法遍歷集合,必須要講集合轉換成數組
}
NSLog(@"*******************");
for(NSString *str in set1)
{
    NSLog(@"%@",str);
}
NSLog(@"*******************");
//枚舉器遍歷
NSEnumerator *e = [set1 objectEnumerator];//objectEnumerator是NSSet中的方法
while (str = [e nextObject]) {//nextObject是NSEnumerator中的方法
    NSLog(@"%@",str);
}

2.NSMutableSet

2.1可變集合,是NSSet的子類
2.2創建方法

NSMutableSet *set1 = [NSMutableSet set];//空集合,有意義,不適合多數量的集合效率低
NSMutableSet *set2 = [NSMutableSet setWithCapacity:100];//預估值
NSMutableSet *set3 = [NSMutableSet setWithObjects:@"one",@"two",@"three", nil];

2.3添加方法

[set3 addObject:@"four"];//addObject也會調用粗慮和細慮,如果元素已經存在,則不會添加
NSLog(@"%@",set3);
[set3 addObject:@"four"];
NSLog(@"%@",set3);

NSArray *addArray = @[@"five",@"six"];
[set3 addObjectsFromArray:addArray];//批量添加
NSLog(@"%@",set3);

2.4刪除方法

[set3 removeObject:@"three"];//刪除指定元素
NSLog(@"%@",set3);
[set3 removeAllObjects];//清空集合
NSLog(@"%@",set3);

2.5運算方法

//交集運算
NSArray *added1 = @[@"one",@"two",@"three"];
[set2 removeAllObjects];
[set2 addObjectsFromArray:added1];
NSArray *added2 = @[@"one",@"three",@"four"];
[set3 removeAllObjects];
[set3 addObjectsFromArray:added2];
[set2 intersectSet:set3];//交集的運算結果被放回到set2
NSLog(@"%@",set2);
//並集運算
[set2 unionSet:set3];
NSLog(@"%@",set2);
//從一個集合中刪除另一個集合的元素
[set2 removeAllObjects];
[set2 addObjectsFromArray:added1];
[set3 removeAllObjects];
[set3 addObjectsFromArray:added2];
[set2 minusSet:set3];
NSLog(@"%@",set2);

思考練習

有一座城市(city),有兩個電影院(cinema),每個電影院有兩個放映廳(video hall),每個放映廳有兩個座位(audience)。由於電影觀衆的增多,添加了一座電影院,原來的電影院又各自增加了一個放映廳。要求用集合表示城市、電影院、放映廳,遍歷所有觀衆信息

SHAudience類:

#import <Foundation/Foundation.h>

@interface SHAudience : NSObject
@property NSString *name;
@property int age;
-(id)initWithName:(NSString*)name andAge:(int)age;
+(id)audienceWithName:(NSString*)name andAge:(int)age;
-(void)show;
@end
#import "SHAudience.h"

@implementation SHAudience
-(id)initWithName:(NSString *)name andAge:(int)age
{
    if (self = [super init])
    {
        self.name = name;
        self.age = age;
    }
    return self;
}
+(id)audienceWithName:(NSString *)name andAge:(int)age
{
    __autoreleasing SHAudience *a = [[SHAudience alloc] initWithName:name andAge:age];
    return a;
}
-(void)show
{
    NSLog(@"姓名:%@,年齡:%d", self.name, self.age);
}
-(NSString *)description
{
    return [NSString stringWithFormat:@"姓名:%@,年齡:%d", self.name, self.age];
}
-(NSUInteger)hash
{
    return self.age;
}
-(BOOL)isEqual:(id)object
{
    if (self == object)
    {
        return YES;
    }
    if ([object isMemberOfClass:[self class]] == YES)
    {
        SHAudience *a = object;
        if ([self.name isEqualToString:a.name] == YES && self.age == a.age)
        {
            return YES;
        }
    }
    return NO;
}
@end

zhangsan.h:

#ifndef zhangsan_h
#define zhangsan_h

NSMutableArray* inputAudienceInfo(int number);

#endif /* zhangsan_h */

zhangsan.m:

#import <Foundation/Foundation.h>
#import "TRAudience.h"

NSMutableArray* inputAudienceInfo(int number)
{
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:number];
    for (int i = 0; i < number; i++)
    {
        NSString *name = [NSString stringWithFormat:@"觀衆%d", i + 1];
        int age = rand() % 41 + 20;
        TRAudience *a = [TRAudience audienceWithName:name andAge:age];
        [array addObject:a];
    }
    return array;
}

main函數:

#import <Foundation/Foundation.h>
#import "SHAudience.h"
#import "zhangsan.h"

typedef enum
{
    AUDIENCE_NUM = 20, VIDEOHALL_NUM = 10, CINEMA_NUM = 4,
}Numbers;

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        srand((unsigned)time(0));

        NSString *str = [NSString stringWithContentsOfFile:@"/Users/tarena/Desktop/number" encoding:NSUTF8StringEncoding error:nil];
        NSArray *data = [str componentsSeparatedByString:@" "];
        NSLog(@"%@", data);
        int audienceNum = [data[0] intValue];

        NSMutableArray *array =  inputAudienceInfo(audienceNum);

        NSMutableArray *videoHall = [NSMutableArray arrayWithCapacity:VIDEOHALL_NUM];
        for (int i = 0; i < VIDEOHALL_NUM; i++)
        {
            NSMutableSet *vh = [NSMutableSet setWithObjects:array[2 * i], array[2 * i + 1], nil];
            [videoHall addObject:vh];
        }

        NSMutableArray *cinema = [NSMutableArray arrayWithCapacity:CINEMA_NUM];
        for (int i = 0; i < CINEMA_NUM; i++)
        {
            NSMutableSet *c = [NSMutableSet setWithObjects:videoHall[2 * i], videoHall[2 * i + 1], nil];
            [cinema addObject:c];
        }
        [cinema[0] addObject:videoHall[VIDEOHALL_NUM - 2]];
        [cinema[1] addObject:videoHall[VIDEOHALL_NUM - 1]];

        NSMutableSet *city = [NSMutableSet setWithCapacity:CINEMA_NUM];
        for (int i = 0; i < CINEMA_NUM; i++)
        {
            [city addObject:cinema[i]];
        }

        for (NSMutableSet *cinema in city)
        {
            for (NSMutableSet *videoHall in cinema)
            {
                for (SHAudience *a in videoHall)
                {
                    NSLog(@"%@", a);
                }
            }
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章