-
Shuffling an NSArray
Posted on April 23rd, 2009 14 commentsFor a project I am working on I needed to shuffle the contents of an NSArray without harming the items themselves. NSArray is a convenient container because it does not care about what you put inside. This is because you don’t (put objects into arrays), you only pretend.
You cannot add an object itself into an array but instead you always insert pointers to class instances. NSArray and its bigger cousin NSMutableArray will keep track of the pointers and memory management of the items so you don’t have to. This is a custom category for NSArray that is useful for shuffling the contents, regardless of their class type.
The header NSArray+Helpers.h:
The implementation NSArray+Helpers.m:
#import "NSArray+Helpers.h" @implementation NSArray (Helpers) - (NSArray *) shuffled { // create temporary autoreleased mutable array NSMutableArray *tmpArray = [NSMutableArray arrayWithCapacity:[self count]]; for (id anObject in self) { NSUInteger randomPos = arc4random()%([tmpArray count]+1); [tmpArray insertObject:anObject atIndex:randomPos]; } return [NSArray arrayWithArray:tmpArray]; // non-mutable autoreleased copy } @end
The implemented method works by adding the object references to a temporary NSMutableArray at random positions. The arithmetic is necessary that only valid positions are chosen at random. If you try to insert an object with too high an index then your app crashes with an exception.
How to use it:
#import "NSArray+Helpers.h" // test shuffling NSArray *arr = [[NSArray arrayWithObjects:@"1",@"2", @"3", @"4", nil] shuffled]; NSLog([arr description]);
You might have noticed previously that I have grown very fond of Class Categories. This is the method of choice if you want to teach existing classes new tricks.
5 responses to “Shuffling an NSArray”

9 Trackbacks / Pingbacks
-
[...] Oliver’s blog post ‘Shuffling an NSArray‘ came across my twitter feed. I’m not sure I followed the link with the intention of [...]
-
[...] to randomize an array. Roy found a couple of examples of this, but one that stood out was over at Dr. Touch’s website. He describes an approach with which to implement a class extension method so you can easily [...]
-
12 Hours, 2 Guys, 6 Cups of Coffee, 1 iPhone App [IPhone] | TechBlogs Today December 29th, 2009 at 22:10
[...] to randomize an array. Roy found a couple of examples of this, but one that stood out was over at Dr. Touch’s website. He describes an approach with which to implement a class extension method so you can easily [...]
-
12 Hours, 2 Guys, 6 Cups of Coffee, 1 iPhone App [IPhone] | Products & Tech News December 29th, 2009 at 23:14
[...] to randomize an array. Roy found a couple of examples of this, but one that stood out was over at Dr. Touch's website. He describes an approach with which to implement a class extension method so you can easily [...]
-
12 Hours, Two Guys, Six Cups Of Coffee, One iPhone App | Gizmodo Australia December 30th, 2009 at 00:47
[...] to randomise an array. Roy found a couple of examples of this, but one that stood out was over at Dr Touch’s website. He describes an approach with which to implement a class extension method so you can easily [...]
-
How 12 Hours, 2 Guys, 6 Cups of Coffee = 1 iPhone App [IPhone] | Products & Tech News December 30th, 2009 at 03:27
[...] to randomize an array. Roy found a couple of examples of this, but one that stood out was over at Dr. Touch's website. He describes an approach with which to implement a class extension method so you can easily [...]
-
[...] to randomize an array. Roy found a couple of examples of this, but one that stood out was over at Dr. Touch’s website. He describes an approach with which to implement a class extension method so you can easily [...]
-
How 12 Hours, 2 Guys, 6 Cups of Coffee = 1 iPhone App | Human Interface Systems December 30th, 2009 at 12:01
[...] to randomize an array. Roy found a couple of examples of this, but one that stood out was over at Dr. Touch’s website. He describes an approach with which to implement a class extension method so you can easily [...]
-
[...] to randomize an array. Roy found a couple of examples of this, but one that stood out was over at Dr. Touch’s website. He describes an approach with which to implement a class extension method so you can easily [...]
-



amccloud April 23rd, 2009 at 10:35