Creating an Ordered Set

+ orderedSet

Creates and returns an empty ordered set.

Example

NSOrderedSet *orderedSet = [NSOrderedSet orderedSet];
NSLog(@"%@",orderedSet);
		

Output

2014-04-16 06:28:48.704 iOS-Tutorial[2007:a0b] {(
)}
		

+ orderedSetWithArray:

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

Example

NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:@[@1,@2,@3,@1]];
NSLog(@"%@",orderedSet);
		

Output

2014-04-16 06:30:04.976 iOS-Tutorial[2029:a0b] {(
    1,
    2,
    3
)}		

+ orderedSetWithArray: range: copyItems:

Creates and returns a new ordered set for a specified range of objects in an array.

Example

NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:@[@1,@2,@3,@1] range:NSMakeRange(0, 2) copyItems:YES];
NSLog(@"%@",orderedSet);
		

Output

2014-04-16 06:30:56.882 iOS-Tutorial[2042:a0b] {(
    1,
    2
)}
		

+ orderedSetWithObject:

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

Example

NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithObject:@2];
NSLog(@"%@",orderedSet);
		

Output

2014-04-16 06:31:35.510 iOS-Tutorial[2054:a0b] {(
    2
)}
		

+ orderedSetWithObjects:

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

Example

NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithObjects:@2,@1,@1,nil];
NSLog(@"%@",orderedSet);
		

Output

2014-04-16 06:32:22.817 iOS-Tutorial[2066:a0b] {(
    2,
    1
)}
		

+ orderedSetWithObjects: count:

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

Example

id array[3] = {@2,@1,@1};
NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithObjects:array count:3 ];
NSLog(@"%@",orderedSet);
		

Output

2014-04-16 06:35:59.461 iOS-Tutorial[2086:a0b] {(
    2,
    1
)}
		

+ orderedSetWithOrderedSet:

Creates and returns an ordered set containing the objects from another ordered set.

Example

NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithOrderedSet:[NSOrderedSet orderedSetWithObject:@1]];
NSLog(@"%@",orderedSet);
		

Output

2014-04-16 06:36:55.384 iOS-Tutorial[2101:a0b] {(
    1
)}
		

+ orderedSetWithOrderedSet: range: copyItems:

Creates and returns a new ordered set for a specified range of objects in an ordered set.

Example

NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithOrderedSet:[NSOrderedSet orderedSetWithObject:@1] range:NSMakeRange(0, 1) copyItems:YES];
NSLog(@"%@",orderedSet);
		

Output

2014-04-16 06:37:54.494 iOS-Tutorial[2112:a0b] {(
    1
)}
		

+ orderedSetWithSet:

Creates and returns an ordered set with the contents of a set.

Example

NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithSet:[NSSet setWithObjects:@2,@1,@2, nil]];
NSLog(@"%@",orderedSet);
		

Output

2014-04-16 06:39:17.815 iOS-Tutorial[2127:a0b] {(
    2,
    1
)}
		

+ orderedSetWithSet: copyItems:

Creates and returns an ordered set with the contents of a set, optionally copying the items.

Example

NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithSet:[NSSet setWithObjects:@2,@1,@2, nil] copyItems:YES];
NSLog(@"%@",orderedSet);
		

Output

2014-04-16 06:39:48.536 iOS-Tutorial[2140:a0b] {(
    2,
    1
)}
		

Initializing an Ordered Set

- initWithArray:

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

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithArray:@[@2,@1,@2]];
NSLog(@"%@",orderedSet);
		

Output

2014-04-16 06:41:22.662 iOS-Tutorial[2152:a0b] {(
    2,
    1
)}
		

- initWithArray: copyItems:

Initializes a newly allocated set with the objects that are contained in a given array, optionally copying the items.

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithArray:@[@2,@1,@2] copyItems:YES];
NSLog(@"%@",orderedSet);
		

Output

2014-04-16 06:41:49.930 iOS-Tutorial[2165:a0b] {(
    2,
    1
)}
		

- initWithArray: range: copyItems:

Initializes a newly allocated set with the objects that are contained in the specified range of an array, optionally copying the items.

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithArray:@[@2,@1,@2] range:NSMakeRange(0, 1) copyItems:YES];
NSLog(@"%@",orderedSet);
		

Output

2014-04-16 06:42:34.934 iOS-Tutorial[2181:a0b] {(
    2
)}
		

- initWithObject:

Initializes a new ordered set with the object.

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithObject:@1];
NSLog(@"%@",orderedSet);
		

Output

2014-04-16 06:43:12.519 iOS-Tutorial[2194:a0b] {(
    1
)}
		

- initWithObjects:

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

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithObjects:@1,@2,nil];
NSLog(@"%@",orderedSet);
		

Output

2014-04-16 06:43:40.082 iOS-Tutorial[2207:a0b] {(
    1,
    2
)}
		

- initWithObjects: count:

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

Example

id array[3] = {@2,@1,@1};
NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithObjects:array count:2];
NSLog(@"%@",orderedSet);
		

Output

2014-04-16 06:44:36.940 iOS-Tutorial[2219:a0b] {(
    2,
    1
)}
		

- initWithOrderedSet:

Initializes a new ordered set with the contents of a set.

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithOrderedSet:[NSOrderedSet orderedSetWithObject:@1]];
NSLog(@"%@",orderedSet);
		

Output

2014-04-16 06:45:55.172 iOS-Tutorial[2233:a0b] {(
    1
)}
		

- initWithOrderedSet: copyItems:

Initializes a new ordered set with the contents of a set, optionally copying the items.

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithOrderedSet:[NSOrderedSet orderedSetWithObject:@1]copyItems:YES];
NSLog(@"%@",orderedSet);
		

Output

2014-04-16 06:45:55.172 iOS-Tutorial[2233:a0b] {(
    1
)}		

- initWithOrderedSet: range: copyItems:

Initializes a new ordered set with the contents of an ordered set, optionally copying the items.

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithOrderedSet:[NSOrderedSet orderedSetWithObject:@1] range:NSMakeRange(0, 1) copyItems:YES];
NSLog(@"%@",orderedSet);
		

Output

2014-04-16 06:47:00.557 iOS-Tutorial[2246:a0b] {(
    1
)}
		

- initWithSet:

Initializes a new ordered set with the contents of a set.

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithSet:[NSSet setWithObjects:@2,@1,@3,nil]];
NSLog(@"%@",orderedSet);
		

Output

2014-04-16 06:48:35.994 iOS-Tutorial[2262:a0b] {(
    3,
    2,
    1
)}
		

- initWithSet: copyItems:

Initializes a new ordered set with the contents of a set, optionally copying the objects in the set.

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithSet:[NSSet setWithObjects:@2,@1,@3,nil] copyItems:YES];
NSLog(@"%@",orderedSet);
		

Output

2014-04-16 06:49:04.832 iOS-Tutorial[2274:a0b] {(
    3,
    2,
    1
)}
		

Counting Entries

- count

Returns the number of members in the set.

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithSet:[NSSet setWithObjects:@2,@1,@3,nil] copyItems:YES];
NSLog(@"%d",[orderedSet count]);
		

Output

2014-04-16 06:49:41.673 iOS-Tutorial[2286:a0b] 3
		

Accessing Set Members

- containsObject:

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

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithSet:[NSSet setWithObjects:@2,@1,@3,nil] copyItems:YES];
NSLog(@"%d",[orderedSet containsObject:@1]);		
	

Output

2014-04-16 06:50:12.265 iOS-Tutorial[2298:a0b] 1
		

- enumerateObjectsAtIndexes: options: usingBlock:

Executes a given block using the objects in the ordered set at the specified indexes.

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithSet:[NSSet setWithObjects:@2,@1,@3,nil] copyItems:YES];
[orderedSet enumerateObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 3)] options:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
	if (idx == [orderedSet count]) {
    	*stop = YES;
	} else {
    	NSLog(@"%@", obj);
    }
}];
		

Output

2014-04-16 06:55:29.961 iOS-Tutorial[2317:a0b] 1
2014-04-16 06:55:29.963 iOS-Tutorial[2317:a0b] 2
2014-04-16 06:55:29.970 iOS-Tutorial[2317:a0b] 3
		

- enumerateObjectsUsingBlock:

Executes a given block using each object in the ordered set.

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithSet:[NSSet setWithObjects:@2,@1,@3,nil] copyItems:YES];
[orderedSet enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
	if (idx == [orderedSet count]) {
     	*stop = YES;
	} else {
		NSLog(@"%@", obj);
	}
}];
		

Output

2014-04-16 06:57:44.711 iOS-Tutorial[2334:a0b] 3
2014-04-16 06:57:44.713 iOS-Tutorial[2334:a0b] 2
2014-04-16 06:57:44.713 iOS-Tutorial[2334:a0b] 1
		

- enumerateObjectsWithOptions:usingBlock:

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

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithSet:[NSSet setWithObjects:@2,@1,@3,nil] copyItems:YES];
[orderedSet enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
	if (idx == [orderedSet count]) {
		*stop = YES;
	} else {
		 NSLog(@"%@", obj);
	}
}];		
	

Output

2014-04-16 06:59:13.828 iOS-Tutorial[2347:a0b] 1
2014-04-16 06:59:13.829 iOS-Tutorial[2347:a0b] 2
2014-04-16 06:59:13.830 iOS-Tutorial[2347:a0b] 3
		

- firstObject

Returns the first object in the ordered set.

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithSet:[NSSet setWithObjects:@2,@1,@3,nil] copyItems:YES];
NSLog(@"%@", [orderedSet firstObject]);
		

Output

2014-04-16 07:00:21.548 iOS-Tutorial[2360:a0b] 3
		

- lastObject

Returns the last object in the ordered set.

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithSet:[NSSet setWithObjects:@2,@1,@3,nil] copyItems:YES];
NSLog(@"%@", [orderedSet lastObject]);
		

Output

2014-04-16 07:00:44.242 iOS-Tutorial[2373:a0b] 1
		

- objectAtIndex:

Returns the object at the specified index of the set.

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithSet:[NSSet setWithObjects:@2,@1,@3,nil] copyItems:YES];
NSLog(@"%@", [orderedSet objectAtIndex:1]);
		

Output

2014-04-16 07:01:10.109 iOS-Tutorial[2385:a0b] 2
		

- objectAtIndexedSubscript:

Returns the object at the specified index of the set.

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithSet:[NSSet setWithObjects:@2,@1,@3,nil] copyItems:YES];
NSLog(@"%@", [orderedSet objectAtIndexedSubscript:1]);
		

Output

2014-04-16 07:01:36.485 iOS-Tutorial[2397:a0b] 2
		

- objectsAtIndexes:

Returns the objects in the ordered set at the specified indexes.

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithSet:[NSSet setWithObjects:@2,@1,@3,nil] copyItems:YES];
NSLog(@"%@", [orderedSet objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)]]);
		

Output

2014-04-16 07:02:26.535 iOS-Tutorial[2411:a0b] (
    3
)
		

- indexOfObject:

Returns the index of the specified object.

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithSet:[NSSet setWithObjects:@2,@1,@3,nil] copyItems:YES];
NSLog(@"%d", [orderedSet indexOfObject:@2]);
		

Output

2014-04-16 07:03:35.511 iOS-Tutorial[2435:a0b] 1
		

- indexOfObject: inSortedRange: options: usingComparator:

Returns the index, within a specified range, of an object compared with elements in the ordered set using a given NSComparator block.

Example

NSOrderedSet *orderedSet = [[NSOrderedSet alloc]initWithSet:[NSSet setWithObjects:@2,@1,@3,nil] copyItems:YES];
 NSLog(@"%d",[orderedSet indexOfObject:@2 inSortedRange:NSMakeRange(0, 3) options:NSBinarySearchingFirstEqual usingComparator:^NSComparisonResult(id obj1, id obj2) {
	return [obj2 compare:obj1];
}]);
		

Output

2014-04-16 07:06:30.485 iOS-Tutorial[2471:a0b] 1
		

Advertisements