Shuffling Arrays, the Objective-C Edition

Shuffling is just like buses. You sit about being deterministic for years, and then all of a sudden, you need some random shuffling on two platforms at once.

As a follow on from my previous post on shuffling AS3 arrays, here’s the same algorithm implemented as a category addition to NSMutableArray for all your easy array shuffling needs.

To use it, download the method and header files, add them to your project and them simply include the header file wherever you you want to shuffle anNSMutableArray:

(example_shuffle.m) download
1
2
3
4
5
6
7
8
9
 #import "NSMutableArray+Shuffle.h"

- (NSMutableArray*)myAwesomeFunction
{
  NSMutableArray* array = [NSMutableArray arrayWithObjects: @"Fred", @"Bob", @"Alice"];
  [array shuffle];

  return array;
}

Grab it while it’s hot from github: