novice needing help on shuffeling an array
Posted
by steffen Myklebust
on Stack Overflow
See other posts from Stack Overflow
or by steffen Myklebust
Published on 2010-03-12T17:38:55Z
Indexed on
2010/03/13
9:15 UTC
Read the original article
Hit count: 212
Please help a TOTAL beginner.!
I found this post: http://stackoverflow.com/questions/56648/whats-the-best-way-to-shuffle-an-nsmutablearray
And as i try to deploy this in my own code, I cant get it working... :-(
Can anyone help me to resolve this code?
To me it looks like the shuffle function is not called..?
here is my code:
// // shuffle2ViewController.h // shuffle2
#import
@interface shuffle2ViewController : UIViewController {
NSMutableArray *puzzles;
int *randomSort;
}
- (void)shuffle;
@end
//=============================
// shuffle2ViewController.m
´#import "shuffle2ViewController.h"
@implementation shuffle2ViewController
(void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *puzzles = [NSMutableArray arrayWithObjects:@"1",@"2",@"3", @"4",@"5",@"6",@"7",@"8",@"9", @"10",@"11",@"12", nil];
// Call the shuffle function
[self shuffle];
// print to log
int i;
NSLog(@"NEW OLD");
NSLog(@"=================");
for (i = 0; i < 12; ++i) NSLog(@" %2i %@", i + 1, [puzzles objectAtIndex:i]); }
int randomSort(id obj1, id obj2, void *context ) {
// returns random number -1 0 1
return (random()%3 - 1); }
(void)shuffle { // call custom sort function
[puzzles sortUsingFunction:randomSort context:nil];
}
Giving this result:
NEW OLD
=================
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
© Stack Overflow or respective owner