Arrays

Things to note

* Arrays must contain objects and cannot contain primitive datatypes. Primitive datatypes can be wrapped in NSData objects.
* Array count is a 1-based value.
* Array object indexes are 0-based values.

! Iterating through every object in an array

for (OCNoteObject *note in notes) {
     NSRect noteBounds = [note Bounds];
}

Get the last object in an array

theObject = [theArray objectAtIndex:([theArray count] - 1)];

Sorting an array

This is only one way to do this, but is the most convenient, in order for an NSArray(or NSMutableArray) to be sorted, and NSSortDescriptor has to be created and applied to the NSArray. The key to using the NSSortDescriptor object is the initWithKey:@“propery_of_object_to_be_sorted” argument. Assuming an array of NSNumber exists, this lines will sort the values in the array in ascending order:

NSSortDescriptor *mySorter = [[NSSortDescriptor alloc] initWithKey:@"intValue" ascending:YES];
[theArray sortUsingDescriptors:[NSArray arrayWithObject:mySorter]];

Source: Macrumors

An actual implementation tucked into a method is here

- (void)sortEmployees
{
	NSSortDescriptor *mySorter = [[NSSortDescriptor alloc] initWithKey:@"personName.self.length" ascending:YES];
	[employees sortUsingDescriptors:[NSArray arrayWithObject:mySorter]];
}

Note the initWithKey:@“personName.self.length” argument. In the Attributes Inspector, you can specify a Sort Key and a Specifier. Sort Key is a path to the property, so personName.self.length would actually be something like

int length = [[personVariable name] length]

Key Paths play a prominent role in building the interface and other operations, but they offer no code hints when using them, which is a bit of a drag.

Sorting an array full of strings

NSArray *dateKeys = [volumeReport allKeys];
NSMutableArray *dateKeysSorted = [NSMutableArray arrayWithArray:dateKeys];

NSSortDescriptor *mySorter = [[NSSortDescriptor alloc] initWithKey:@"" ascending:YES];
[dateKeysSorted sortUsingDescriptors:[NSArray arrayWithObject:mySorter]];

A couple things to note:

  • The array of keys had to be converted into an NSMutableArray so that sorting could happen (gcc returns an error).
  • The initWithKey: argument is empty because we are simply sorting by the values themselves, and not a property of the values.

Shuffle Array Contents

Note the dependency on my own - (int) intInRangeMinimum:(int)min andMaximum:(int)max

- (NSMutableArray *)shuffleArray:(NSArray *)sourceArray {
	
	NSMutableArray *shuffledArray = [NSMutableArray arrayWithArray:sourceArray];
	
	/* 
	 Initialize an array of 1001 integers with the values 0-1000 and set a
	 variable, max, to the current max index of the array (starting with
	 1000). Pick a random number, r, between 0 and max, swap the number at
	 the position r with the number at position max and return the number now
	 at position max. Decrement max by 1 and continue. When max is 0, set max
	 back to the size of the array - 1 and start again without the need to
	 reinitialize the array.
	 */
	
	int i = 0;
	int max = [sourceArray count] - 1;
	
	int rMax = max;
	
	for (i = 0; i <= max; i++) {
		
		int r = [self intInRangeMinimum:0 andMaximum:rMax];
		
		[shuffledArray exchangeObjectAtIndex:rMax withObjectAtIndex:r];
		
		rMax--;
	}
	
	return shuffledArray;
}

Source: Stackoverflow