0

When I Build & Run my application, it will not generate anything. What I have generating are words and after it erases that word and continues until it exhausts all the words and then repopulates the list again. Here is the code:

@implementation randomnumbersViewController
@synthesize words;
@synthesize randomArray;
@synthesize array;

-(IBAction)generateNumber:(id)sender {

    NSInteger randomize(id num1, id num2, void *context);
    int rand = arc4random() %2;
    if (rand)
        return NSOrderedAscending;
    else
        return NSOrderedDescending;
}

- (void)resetRandomArray;
{
    [randomArray setArray: array];
    [randomArray sortUsingFunction:random context:NULL];
}

- (NSString*) getRandomWord; {
    if ([randomArray count] ==0)
        return nil;
    NSString* result;
    NSInteger randomIndex = [[randomArray lastObject] intValue];
    [randomArray removeLastObject];
    result = [words objectAtIndex:randomIndex];
    return result;
}

- (void)buildRandomWordArray
{
    NSInteger index;

    NSError *theError;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"words" ofType:@"text"];
    NSString *text = [NSString stringWithContentsOfFile: path
                                               encoding: NSUTF8StringEncoding
                                                  error: &theError];
    self.words = [text componentsSeparatedByString: @"\n"];

    int arraySize = [words count];
    self.array = [NSMutableArray arrayWithCapacity:arraySize];

    //This code fills "array' with index values from 0 to the number of elements in the     "words" array.
    for (index = 0; index<arraySize; index++)
        [array addObject: [NSNumber numberWithInt: index]];
    [self resetRandomArray];

    //for (index = 0; index<=arraySize; index++)
    // NSLog(@ "Random word: %@", [self getRandomWord]);
}

Also a .txt document must be included in the resources folder in for this to work and I do have it there, but nothing. Does anyone have any suggestions as to how I can actually get it to generate the words, or why it isn't working properly?

jscs
  • 63,694
  • 13
  • 151
  • 195
  • I would be surprised if this code every compiles – Daniel Oct 18 '11 at 20:57
  • Your question is rather unclear, and we are not here to debug your program for you. What exactly is your expected result? What is the actual result? "It will not generate anything" isn't good enough. What exactly appears on the screen when the program runs? Most importantly, what have you done to determine the cause of the problem yourself? – jscs Oct 18 '11 at 21:48
  • The expected result is that it should generate 1 out 4 words in a text document and by doing so it eliminates it from the list and then randomly calls up the remaining items. The process repeats until the list is empty and the list repopulates itself. The actual result is that it doesn't generate anything. I connected it properly and there is no result. What appears on the screen is a button and a label which when you press it, the words are supposed to generate. And I understand that you are not here to debug my code just a gentle nudge in the right direction will be nice. – Luis Rivera Oct 18 '11 at 22:21

1 Answers1

1

I don't get how sorting the array ascending or descending is going to shuffle the array, maybe because it doesn't. :) You should use the Fisher–Yates shuffle implemented here: What's the Best Way to Shuffle an NSMutableArray? Import that category, and just call shuffle on the mutable array.

Community
  • 1
  • 1
Jano
  • 62,815
  • 21
  • 164
  • 192