您的位置:首页 > 其它

OC-FUNDATION-NSSet&NSMutableSet

2016-08-29 11:07 351 查看

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
NSLo
9f28
g(@"%@",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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  NSSet