OC-FUNDATION-NSSet&NSMutableSet

原创 2016年08月29日 11:07:08

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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

黑马程序员 OC语言 - 8 NSFileManager、NSSet、NSMutableSet

若排版有错误,请查看PDF版:http://pan.baidu.com/s/1nt7JHNV

[OC学习笔记]NSSet、NSMutableSet、NSIndexSet常用方法

NSMutableSet是NSSet的子类,它继承了后者的方法1.NSSet常用方法 方法 描述 +(instancetype) setWithObjects: obj1, obj2, ob...

iOS开发-Day19-OC NSDictionary&NSMutableDictionary&NSSet&NSMutableSet

代码说事!(代码中笔记较为详细,因此不单独再列出了!) 关于字典和动态字典 NSDictionary&NSMutableDictionary&NSSet&NSMutableSet...

OC NSSet、NSMutableSet、NSArray、NSMutableArray

不可变长度的Set @interface NSSet : NSObject   初始化方法 + (instancetype)set; //声明一个空集 + (instancetype)setWithO...

oc 知识总结四 (NSSet、NSMutableSet)

注:集合是一组单值对象的集合,同一个对象只能保存一个,且集合中的元素是无序的。 一、NSSet(不可变集合) 1、NSSet的创建 (1)创建一个空集合  NSSet *set = [NSSe...

【IOS 开发学习总结-OC-30】★★OC之foundation 框架——集合(NSSet 与 NSMutableSet)

有关NSSet的概述介绍参见前面的一篇博文: 【IOS 开发学习总结-OC-28】★objective-c之foundation 框架——集合类的综述 NSSet的功能和用法NSSet按 Hash 算...

OC学习之道:OC对象,字典,集合的使用方法NSDictionary,NSMutableDictionary, NSSet,NSMutableSet,NSCountedSet

OC学习之道:OC对象,字典,集合的使用方法NSDictionary,NSMutableDictionary, NSSet,NSMutableSet,NSCountedSetint main(int ...

Objective-C语法之NSSet和NSMutableSet

NSSet和NSMutableSet是无序的, 但是它保证数据的唯一性。当插入相同的数据时,不会有任何效果。从内部实现来说是hash表,所以可以常数时间内查找一个数据。 1、NS...

12、Objective-C语法之NSSet和NSMutableSet

NSSet和NSMutableSet是无序的, 但是它保证数据的唯一性。当插入相同的数据时,不会有任何效果。从内部实现来说是hash表,所以可以常数时间内查找一个数据。 1、NSSet的...
  • zwcshy
  • zwcshy
  • 2014年06月19日 13:02
  • 260

NSSet和NSMutableSet 确保数据的唯一性

NSSet和NSMutableSet是无序的, 但是它保证数据的唯一性。当插入相同的数据时,不会有任何效果。从内部实现来说是hash表,所以可以常数时间内查找一个数据。 1、...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:OC-FUNDATION-NSSet&NSMutableSet
举报原因:
原因补充:

(最多只允许输入30个字)