[Objective-C] integrate basic concepts and common methods in OC (NSSet)

Source: Internet
Author: User
Tags new set

I. Basic concepts of a set

The NSSet class is provided in the Foundation framework. It is a set of Single-value objects and the elements in the NSSet instance are unordered. Only one object can be saved,

It is also divided into variable and immutable set objects (variable set object, NSMutableSet)

2. immutable set-NSSet

1: initialization (like creating an array)

// Create a set directly similar to Array Construction

        NSSet *set1=[[NSSet alloc]initWithObjects:@"one",@"tow", nil];        NSLog(@"%@",set1);

 

2: Build a set through Arrays

 

// Construct NSArray * array1 = [NSArray arrayWithObjects: @ "one", @ "tow", nil] Through arrays; NSSet * set2 = [NSSet setWithArray: array1]; NSLog (@ "% @", set2 );
3: build with an existing set
// Build with an existing set
NSSet *set3=[NSSet setWithSet:set2]; NSLog(@"%@",set3);
3: Number of collection objects
// Common methods in Collection
NSInteger *count=[set3 count]; NSLog(@"%ld",count);
4: return all elements in the set.
// All elements in the Set
NSArray *array2 =[set3 allObjects]; NSLog(@"%@",array2);
5: return any element in the set.
// Returns any element in the set.
NSString *str=[set3 anyObject]; NSLog(@"%@",str);
6: Check whether an element is contained in the collection.
// Query whether an element Boolean result1 = [set3 containsObject: @ "two"]; if (result1) {NSLog (@ "contains two ");} else {NSLog (@ "two not included ");}
7: Check whether there is an intersection between a set and a set.
// Query whether there is an intersection between Sets
BOOL result2= [set1 intersectsSet:set2]; NSLog(@"%d",result2);
8: Set matching
// Determine whether the set matches
BOOL result3=[set1 isEqualToSet:set2]; NSLog(@"%d",result3); 
9: whether it is a subset of a set
// Whether it is a subset of a set
BOOL result4=[set1 isSubsetOfSet:set2]; NSLog(@"%d",result4);
10: Add a new element to a set to return a new set.
     NSSet *set5=[NSSet setWithObjects:@"one",nil]; 
NSSet *appSet=[set5 setByAddingObject:@"tow"]; NSLog(@"%@",appSet); 
11: Add a set to a set and return a new set.
// Add a set to a set
NSSet *set6=[NSSet setWithObjects:@"1",@"2", nil]; NSSet *appSet1=[set5 setByAddingObjectsFromSet:set6]; NSLog(@"%@",appSet1);
12: add an array to a set and return a new set.
// Add a number to a set
NSArray *appArray=[NSArray arrayWithObjects:@"x",@"y", nil]; NSSet *appSet2=[set5 setByAddingObjectsFromArray:appArray]; NSLog(@"%@",appSet2);
Iii. Variable set-NSMutableSet
1: Create an initialized variable set
// Create an initialized variable set
NSMutableSet * mutableSet1 = [NSMutableSet Set]; // empty Set NSMutableSet * mutableSet2 = [NSMutableSet setWithObjects: @ "1", @ "2", nil]; NSMutableSet * mutableSet3 = [NSMutableSet setWithObjects: @ "a", @ "2", nil];
2: remove the same element from the set
// Remove the same part from the two sets
[mutableSet2 minusSet:mutableSet3]; NSLog(@"%@",mutableSet2); 
3: Calculate the public elements of the two sets.
// Obtain the same elements of the two sets.
[mutableSet2 intersectSet:mutableSet3]; NSLog(@"%@",mutableSet2);
4: merge two sets
// Merge the two sets.
[mutableSet2 unionSet:mutableSet3]; NSLog(@"%@",mutableSet2);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.