NSSet和NSMutableSet

來源:互聯網
上載者:User

 

一、簡介

集合(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:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.