NSSet和NSMutableSet

 

一、简介

集合(set)是一组单值对象,它可以是固定的(NSSet)、也可以是可变的(NSMutableSet)。集合可以比较、计算交集、并集,可变集合还可以有查找、添加、删除。

二、常用方法

#import <Foundation/Foundation.h>

int main (int argc, char *argv[])

{

    NSAutoreleasePool*pool=[[NSAutoreleasePool alloc]init];

    //集合比较、修改

    NSSet *set1=[NSSet setWithObjects:@"s1",@"s2",@"s3",@"s4",nil];

    NSMutableSet *set2=[NSMutableSet setWithObjects:@"s1",@"s2",@"s3",@"s4",nil];

    NSLog(@"List set1");

    for (NSString *element in set1) {

        NSLog(@"%@",element);

    }

    if ([set1 isEqualToSet:set2]) {

        NSLog(@"set1 is equal to set set2");

    } else {

        NSLog(@"set1 is not equal to set set2");

    }

    if ([set1 containsObject:@"s1"]) {

        NSLog(@"set1 contains s1");

    } else {

        NSLog(@"set1 not contains s1");

    }

    [set2 addObject:@"s5"];

    [set2 removeObject:@"s3"];

    NSLog(@"List set2");

    for (NSString *element in set2) {

        NSLog(@"%@",element);

    }

    //集合交集、并集

    NSMutableSet *set3;

    set3=[NSMutableSet setWithObjects:@"s1",@"s3",@"s5",nil];

    [set3 intersectSet:set1];

    NSLog(@"135 intersectSet 1234");

    for (NSString *element in set3) {

        NSLog(@"%@",element);

    }

    set3=[NSMutableSet setWithObjects:@"s1",@"s3",@"s5",nil];

    [set3 unionSet:set1];

    NSLog(@"135 unionSet 1234");

    for (NSString *element in set3) {

        NSLog(@"%@",element);

    }

    [pool drain];

    return 0;

}

setWithObjects创建包含给定的对象列表的集合
+ (id)setWithObjects:(id)firstObj ...

isEqualToSet比较两个集合是否相等
- (BOOL)isEqualToSet:(NSSet *)otherSet

containsObject判断给定的对象是否在集合中
- (BOOL)containsObject:(id)anObject

addObject给集合添加一个对象,如果已有这个对象则不会添加
- (void)addObject:(id)object

removeObject删除集合中给定的对象
- (void)removeObject:(id)anObject

intersectSet取两个集合的交集,如果接收集合中的成员不是给定集合的成员,则从接受集合中删除这个成员。
- (void)intersectSet:(NSSet *)otherSet

unionSet取两个集合的并集,如果给定集合中的成员不是接收集合的成员,则将这个成员添加到接收集合中。
- (void)unionSet:(NSSet *)otherSet

三、NSSet的全部方法

Creating a Set
+ set
+ setWithArray:
+ setWithObject:
+ setWithObjects:
+ setWithObjects:count:
+ setWithSet:
– setByAddingObject:
– setByAddingObjectsFromSet:
– setByAddingObjectsFromArray:
Initializing a Set
– initWithArray:
– initWithObjects:
– initWithObjects:count:
– initWithSet:
– initWithSet:copyItems:
Counting Entries
– count
Accessing Set Members
– allObjects
– anyObject
– containsObject:
– filteredSetUsingPredicate:
– makeObjectsPerformSelector:
– makeObjectsPerformSelector:withObject:
– member:
– objectEnumerator
– enumerateObjectsUsingBlock:
– enumerateObjectsWithOptions:usingBlock:
– objectsPassingTest:
– objectsWithOptions:passingTest:
Comparing Sets
– isSubsetOfSet:
– intersectsSet:
– isEqualToSet:
– valueForKey:
– setValue:forKey:
Creating a Sorted Array
– sortedArrayUsingDescriptors:
Key-Value Observing
– addObserver:forKeyPath:options:context:
– removeObserver:forKeyPath:context:
– removeObserver:forKeyPath:
Describing a Set
– description
– descriptionWithLocale:

四、NSMutableSet的全部方法

Creating a Mutable Set
+ setWithCapacity:
– initWithCapacity:
Adding and Removing Entries
– addObject:
– filterUsingPredicate:
– removeObject:
– removeAllObjects
– addObjectsFromArray:
Combining and Recombining Sets
– unionSet:
– minusSet:
– intersectSet:
– setSet:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值