0

so I have this method that returns a letter (from A-Z) by creating a random number between 1-26 and then indexing a string array to pull out the letter. The only problem is that it does not generate a new letter when the method is called. How can I generate a new letter every time the method is called, like in a while loop. Here is my code:

-(NSString *)alphaGenerator{
    NSUInteger randomLetterInteger = arc4random_uniform(26);
    NSArray *alphabet = @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"];
    NSString *alpha = alphabet[randomLetterInteger];
    return alpha;
}

Also, how do I convert a number that is returned from the count method into a number that I can plug into the arc4random_uniform method? I receive the 'incompatible integer to pointer inversion initializing...' error. This is what I have for this:

-(NSUInteger)numOfDictions{
    NSString *alpha = [self alphaGenerator];
    NSUInteger numb = [cuisines[alpha] count];
    NSUInteger *randomNumOfDictions = arc4random_uniform(numb);
    return *randomNumOfDictions;
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • what are you trying to do with randomNumOfDictions? You're assigning a random number to a pointer, then dereferencing and returning whatever garbage it points to? That doesn't make any sense. Looks like alphaGenerator should be working. Try calling it repeatedly from a loop and logging its output. I think the problem is in your numOfDictations method--it's not obvious what it's trying to do, but I'm pretty sure it's broken. – Nicholas Hart May 29 '14 at 19:59
  • 1
    Get rid of the asterisks next to each `randomNumOfDictions` in your `numOfDictions` method. – rmaddy May 29 '14 at 20:01
  • I have a dictionary, cuisines, that has 26 arrays(A-Z). In each of these arrays are dictionaries that each hold 2 strings, name and description. I am trying to first, count the number of dictionaries in a random letter array that is chosen by alphaGenerator. Then I want to create and return a random number that is the maximum of the number of dictionaries....@rmaddy, yeah I have no idea why those were in there. Thanks – leeferfeefer May 29 '14 at 20:05
  • @MartinR That duplicate has nothing to do with this question. – rmaddy May 29 '14 at 20:07
  • @rmaddy: The question has (at least) two parts (which is always bad). The **first part** (which is also mentioned in the title) *"How can I generate a new letter every time the method is called"* it clearly about creating random elements without repetition and should be answered in the duplicate. - But I will reopen it before arguing about that. – Martin R May 29 '14 at 20:09
  • @MartinR There's a big difference between "generate a new letter" and "generate without duplicates". – rmaddy May 29 '14 at 20:11
  • Well, to me the first part of the question looks like an exact duplicate of [Objective-C, Getting a random object from NSArray without duplication](http://stackoverflow.com/questions/16791343/objective-c-getting-a-random-object-from-nsarray-without-duplication). – Martin R May 29 '14 at 20:15

1 Answers1

1

These two lines:

NSUInteger *randomNumOfDictions = arc4random_uniform(numb);
return *randomNumOfDictions;

should be:

NSUInteger randomNumOfDictions = arc4random_uniform(numb);
return randomNumOfDictions;

NSInteger is not an object type. It's a primitive type.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • So the error it still gives for the loss of integer precision doesn't matter? – leeferfeefer May 29 '14 at 20:09
  • Use a cast if needed to deal with the precision issue. – rmaddy May 29 '14 at 20:10
  • I did, thanks a lot! turns out i was doing everything correct, I just wasn't implementing the alphaGenerator method correctly in my loop. I just called the method instead of assigning it to a string. Thanks a ton guys! – leeferfeefer May 29 '14 at 20:59