Creating a Set

+ set

Creates and returns an empty set.

Example

	NSSet *set = [NSSet set];
    NSLog(@"%@",set);
		

Output

2014-03-22 18:48:05.304 iOS-Tutorial[2915:a0b] {(
)}
		

+ setWithArray:

Creates and returns a set containing a uniqued collection of the objects contained in a given array.

Example

	NSSet *set = [NSSet setWithArray:@[@"Eezy",@"Tutorials"]];
    NSLog(@"%@",set);
		

Output

2014-03-22 18:49:50.290 iOS-Tutorial[2934:a0b] {(
    Eezy,
    Tutorials
)}
		

+ setWithObject:

Creates and returns a set that contains a single given object.

Example

	NSSet *set = [NSSet setWithObject:@"Eezy"];
    NSLog(@"%@",set);
		

Output

2014-03-22 18:50:38.802 iOS-Tutorial[2952:a0b] {(
    Eezy
)}
		

+ setWithObjects:

Creates and returns a set containing the objects in a given argument list.

Example

	NSSet *set = [NSSet setWithObjects:@"Eezy",@"Tutorials",nil];
    NSLog(@"%@",set);
		

Output

2014-03-22 18:51:27.307 iOS-Tutorial[2969:a0b] {(
    Eezy,
    Tutorials
)}
		

+ setWithObjects:count:

Creates and returns a set containing a specified number of objects from a given C array of objects.

Example

	NSString *values[3];
    values[0] = @"Eezy";
    values[1] = @"Tutorials";
    values[2] = @"Website";
	NSSet *set = [NSSet setWithObjects:values count:3];
    NSLog(@"%@",set);
		

Output

2014-03-22 18:53:26.916 iOS-Tutorial[2984:a0b] {(
    Eezy,
    Tutorials,
    Website
)}
		

+ setWithSet:

Creates and returns a set containing the objects from another set.

Example

	NSSet *tempSet = [NSSet setWithObject:@[@"Eezy"]];
	NSSet *set = [NSSet setWithSet:tempSet];
    NSLog(@"%@",set);
		

Output

2014-03-22 18:55:27.174 iOS-Tutorial[3001:a0b] {(
        (
        Eezy
    )
)}
		

- setByAddingObject:

Returns a new set formed by adding a given object to the receiving set.

Example

	NSSet *set = [NSSet setWithObject:@"Eezy"];
    set = [set setByAddingObject:@"Tutorials"];
    NSLog(@"%@",set);
		

Output

2014-03-22 18:58:32.858 iOS-Tutorial[3045:a0b] {(
    Eezy,
    Tutorials
)}
		

- setByAddingObjectsFromSet:

Returns a new set formed by adding the objects in a given set to the receiving set.

Example

	NSSet *tempSet = [NSSet setWithObject:@"Tutorials"];
	NSSet *set = [NSSet setWithObject:@"Eezy"];
    set = [set setByAddingObjectsFromSet:tempSet];
    NSLog(@"%@",set);
		

Output

2014-03-22 19:01:01.822 iOS-Tutorial[3060:a0b] {(
    Eezy,
    Tutorials
)}
		

- setByAddingObjectsFromArray:

Returns a new set formed by adding the objects in a given array to the receiving set.

Example

	NSSet *set = [NSSet setWithObject:@"Eezy"];
    set = [set setByAddingObjectsFromArray:@[@"Tutorials",@"Website"]];
    NSLog(@"%@",set);
		

Output

2014-03-22 19:02:20.419 iOS-Tutorial[3074:a0b] {(
    Eezy,
    Tutorials,
    Website
)}
		

Initializing a Set

- initWithArray:

Initializes a newly allocated set with the objects that are contained in a given array.

Example

	NSSet *set = [[NSSet alloc ]initWithArray:@[@"Tutorials",@"Website"]];
    NSLog(@"%@",set);
		

Output

2014-03-22 19:04:11.145 iOS-Tutorial[3089:a0b] {(
    Tutorials,
    Website
)}
		

- initWithObjects:

Initializes a newly allocated set with members taken from the specified list of objects.

Example

	NSSet *set = [[NSSet alloc ]initWithObjects:@"Tutorials",@"Website",nil];
    NSLog(@"%@",set);
		

Output

2014-03-22 19:05:06.596 iOS-Tutorial[3101:a0b] {(
    Tutorials,
    Website
)}
		

- initWithObjects:count:

Initializes a newly allocated set with a specified number of objects from a given C array of objects.

Example

	NSString *values[3];
    values[0] = @"Eezy";
    values[1] = @"Tutorials";
    values[2] = @"Website";
	NSSet *set = [[NSSet alloc ]initWithObjects:values count:3];
    NSLog(@"%@",set);
		

Output

2014-03-22 19:06:53.094 iOS-Tutorial[3113:a0b] {(
    Eezy,
    Tutorials,
    Website
)}
		

- initWithSet:

Initializes a newly allocated set and adds to it objects from another given set.

Example

	NSSet *tempSet = [NSSet setWithObject:@"Tutorials"];
   	NSSet *set = [[NSSet alloc ]initWithSet:tempSet];
    NSLog(@"%@",set);
		

Output

2014-03-22 19:08:14.078 iOS-Tutorial[3129:a0b] {(
    Tutorials
)}
		

- initWithSet:copyItems:

Initializes a newly allocated set and adds to it members of another given set.

Example

	NSSet *tempSet = [NSSet setWithObject:@"Tutorials"];
   	NSSet *set = [[NSSet alloc ]initWithSet:tempSet copyItems:YES];
    NSLog(@"%@",set);
		

Output

2014-03-22 19:08:14.078 iOS-Tutorial[3129:a0b] {(
    Tutorials
)}
		

- init

Initializes a newly allocated set.

Example

	NSSet *set = [[NSSet alloc ]init];
    NSLog(@"%@",set);
		

Output

2014-03-22 19:09:08.835 iOS-Tutorial[3141:a0b] {(
)}
		

Counting Entries

- count

Returns the number of members in the set.

Example

	NSSet *set = [NSSet setWithObjects:@"Tutorials",@"Website",nil];
    NSLog(@"Count: %d",[set count]);
		

Output

2014-03-22 19:11:27.729 iOS-Tutorial[3155:a0b] Count: 2
		

Accessing Set Members

- allObjects

Returns an array containing the set’s members, or an empty array if the set has no members.

Example

	NSSet *set = [NSSet setWithObjects:@"Tutorials",@"Website",nil];
    NSLog(@"%@",[set allObjects]);
		

Output

2014-03-22 19:16:49.741 iOS-Tutorial[3169:a0b] (
    Tutorials,
    Website
)
		

- anyObject

Returns one of the objects in the set, or nil if the set contains no objects.

Example

	NSSet *set = [NSSet setWithObjects:@"Tutorials",@"Website",nil];
    NSLog(@"%@",[set anyObject]);
		

Output

2014-03-22 19:17:34.821 iOS-Tutorial[3193:a0b] Tutorials
		

- containsObject:

Returns a Boolean value that indicates whether a given object is present in the set.

Example

	NSSet *set = [NSSet setWithObjects:@"Tutorials",@"Website",nil];
    NSLog(@"Contains: %d",[set containsObject:@"Website"]);
		

Output

2014-03-22 19:18:42.372 iOS-Tutorial[3203:a0b] Contains: 1
		

- filteredSetUsingPredicate:

Evaluates a given predicate against each object in the receiving set and returns a new set containing the objects for which the predicate returns true.

Example

	NSSet *set = [NSSet setWithObjects:@"Eezy",@"Tutorials",@"Website",nil];
    NSPredicate *predicate =
    [NSPredicate predicateWithFormat:@"SELF beginswith[c] 'E'"];
    set = [set filteredSetUsingPredicate:predicate];
    NSLog(@"%@", set);
		

Output

2014-03-22 19:23:40.083 iOS-Tutorial[3227:a0b] {(
    Eezy
)}
		

- objectEnumerator

Returns an enumerator object that lets you access each object in the set.

Example

	NSSet *set = [NSSet setWithObjects:@"Eezy",@"Tutorials",@"Website",nil];
    NSEnumerator *enumerator = [set objectEnumerator];
    id obj;
    while (obj =[ enumerator nextObject]) {
        NSLog(@"%@",obj);
    }
		

Output

2014-03-22 19:42:27.269 iOS-Tutorial[3322:a0b] Eezy
2014-03-22 19:42:27.271 iOS-Tutorial[3322:a0b] Tutorials
2014-03-22 19:42:27.271 iOS-Tutorial[3322:a0b] Website
		

- enumerateObjectsUsingBlock:

Executes a given Block using each object in the set.

Example

	NSSet *set = [NSSet setWithObjects:@"Eezy",@"Tutorials",@"Website",nil];
    
    [set enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
        NSLog(@"%@",obj);
    }];
		

Output

2014-03-22 19:44:12.411 iOS-Tutorial[3334:a0b] Eezy
2014-03-22 19:44:12.412 iOS-Tutorial[3334:a0b] Tutorials
2014-03-22 19:44:12.413 iOS-Tutorial[3334:a0b] Website
		

- enumerateObjectsWithOptions: usingBlock:

Executes a given Block using each object in the set, using the specified enumeration options.

Example

	NSSet *set = [NSSet setWithObjects:@"Eezy",@"Tutorials",@"Website",nil];
    
    [set enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, BOOL *stop) {
        NSLog(@"%@",obj);
    }];
		

Output

2014-03-22 19:45:03.978 iOS-Tutorial[3349:a0b] Eezy
2014-03-22 19:45:03.979 iOS-Tutorial[3349:a0b] Tutorials
2014-03-22 19:45:03.980 iOS-Tutorial[3349:a0b] Website
		

- objectsPassingTest:

Returns a set of object that pass a test in a given Block.

Example

	NSSet *set = [NSSet setWithObjects:@"Eezy",@"Tutorials",@"Website",nil];
    set = [set objectsPassingTest:^BOOL(id obj, BOOL *stop) {
        return ![obj isEqualToString:@"Eezy"];
    }];
    NSLog(@"%@",set);
		

Output

2014-03-22 19:47:25.433 iOS-Tutorial[3384:a0b] {(
    Tutorials,
    Website
)}
		

- objectsWithOptions:passingTest:

Returns a set of object that pass a test in a given Block, using the specified enumeration options.

Example

	NSSet *set = [NSSet setWithObjects:@"Eezy",@"Tutorials",@"Website",nil];
    set = [set objectsWithOptions:NSEnumerationReverse passingTest:^BOOL(id obj, BOOL *stop) {
        return ![obj isEqualToString:@"Eezy"];
    }];
    NSLog(@"%@",set);
		

Output

2014-03-22 19:48:35.759 iOS-Tutorial[3395:a0b] {(
    Tutorials,
    Website
)}
		

Comparing Sets

- isSubsetOfSet:

Returns a Boolean value that indicates whether every object in the receiving set is also present in another given set.

Example

	NSSet *set1 = [NSSet setWithObject:@"Tutorials"];
	NSSet *set2 = [NSSet setWithObjects:@"Eezy",@"Tutorials",@"Website",nil];
    NSLog(@"Subset %d",[set1 isSubsetOfSet:set2]);
		

Output

2014-03-22 19:50:30.712 iOS-Tutorial[3407:a0b] Subset 1
		

- intersectsSet:

Returns a Boolean value that indicates whether at least one object in the receiving set is also present in another given set.

Example

	NSSet *set1 = [NSSet setWithObject:@"Tutorials"];
	NSSet *set2 = [NSSet setWithObjects:@"Eezy",@"Tutorials",@"Website",nil];
    NSLog(@"Intersect %d",[set1 intersectsSet:set2]);
		

Output

2014-03-22 19:51:57.172 iOS-Tutorial[3436:a0b] Intersect 1
		

- isEqualToSet:

Compares the receiving set to another set.

Example

	NSSet *set1 = [NSSet setWithObjects:@"Tutorials",@"Eezy",nil];
	NSSet *set2 = [NSSet setWithObjects:@"Eezy",@"Tutorials",nil];
    NSLog(@"isEqual: %d",[set1 isEqualToSet:set2]);
		

Output

2014-03-22 19:53:11.059 iOS-Tutorial[3451:a0b] isEqual: 1
		

Creating a Sorted Array

- sortedArrayUsingDescriptors:

Returns an array of the set’s content sorted as specified by a given array of sort descriptors.

Example

	NSDictionary *dict1 = @{@"key1":@"iOS",@"key2": @"Tutorials", @"key3":@"Website"};
    NSDictionary *dict2 = @{@"key1":@"Eezy",@"key2": @"Tutorials", @"key3":@"Website"};
    NSSortDescriptor * descriptor =[[NSSortDescriptor alloc] 
		initWithKey:@"key1" ascending:YES];
    NSSet *set = [NSSet setWithObjects:dict1,dict2,nil];
    NSArray *resultArray = [set sortedArrayUsingDescriptors:@[descriptor]];
    NSLog(@"%@",resultArray);
		

Output

2014-03-22 19:57:45.534 iOS-Tutorial[3486:a0b] (
        {
        key1 = Eezy;
        key2 = Tutorials;
        key3 = Website;
    },
        {
        key1 = iOS;
        key2 = Tutorials;
        key3 = Website;
    }
)
		

Advertisements