0

I'm totally new to iOS development so forgive me if this question is stupid, but its wrecking my head.

I've set myself the task of building a Hangman Game for iPhone. When the game starts I want the letters of the alphabet to randomly populate into 26 UIImageView's that I have already setup in my ViewController.

I have 2 NSMutableArray's setup. One holds the UIImage reference for the Alphabet images and the other is an array of strings containing the names of all the UIImageViews.

What I'm looking to do is run through a for or a while loop and have the images assigned to the UIImageViews.

Here is the code I'm currently using.

- (void)randomizeAlphabet
{
    // Code to randomise Letters on start up of the App.
    NSMutableArray *alphabet = [[NSMutableArray alloc] initWithObjects:
                     [UIImage imageNamed:@"A.png"],
                     [UIImage imageNamed:@"B.png"],
                     [UIImage imageNamed:@"C.png"],
                     [UIImage imageNamed:@"D.png"],
                     [UIImage imageNamed:@"E.png"],
                     [UIImage imageNamed:@"F.png"],
                     [UIImage imageNamed:@"G.png"],
                     [UIImage imageNamed:@"H.png"],
                     [UIImage imageNamed:@"I.png"],
                     [UIImage imageNamed:@"J.png"],
                     [UIImage imageNamed:@"K.png"],
                     [UIImage imageNamed:@"L.png"],
                     [UIImage imageNamed:@"M.png"],
                     [UIImage imageNamed:@"N.png"],
                     [UIImage imageNamed:@"O.png"],
                     [UIImage imageNamed:@"P.png"],
                     [UIImage imageNamed:@"Q.png"],
                     [UIImage imageNamed:@"R.png"],
                     [UIImage imageNamed:@"S.png"],
                     [UIImage imageNamed:@"T.png"],
                     [UIImage imageNamed:@"U.png"],
                     [UIImage imageNamed:@"V.png"],
                     [UIImage imageNamed:@"W.png"],
                     [UIImage imageNamed:@"X.png"],
                     [UIImage imageNamed:@"Y.png"],
                     [UIImage imageNamed:@"Z.png"],
                     nil];

NSMutableArray *imgViewsAlphabet = [[NSMutableArray alloc] initWithObjects:
                                    @"letterA",
                                    @"letterB",
                                    @"letterC",
                                    @"letterD",
                                    @"letterE",
                                    @"letterF",
                                    @"letterG",
                                    @"letterH",
                                    @"letterI",
                                    @"letterJ",
                                    @"letterK",
                                    @"letterL",
                                    @"letterM",
                                    @"letterN",
                                    @"letterO",
                                    @"letterP",
                                    @"letterQ",
                                    @"letterR",
                                    @"letterS",
                                    @"letterT",
                                    @"letterU",
                                    @"letterV",
                                    @"letterW",
                                    @"letterX",
                                    @"letterY",
                                    @"letterZ"
                                    , nil];
//Randomly assign the letter from the array
while ([alphabet count] > 0 )
{
    int index = arc4random_uniform([alphabet count]);
    [(UIImageView *)[imgViewsAlphabet objectAtIndex:index] setImage:[alphabet objectAtIndex:index]];
    [alphabet removeObjectAtIndex:index];
    [imgViewsAlphabet removeObjectAtIndex:index];
}
}

The code above seems fine in Xcode but when it compiles it crashes out.

Any help at all would be greatly appreciated.

Cheers, TQ

Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92

2 Answers2

1

You have 2 arrays, imgViewsAlphabet and alphabet

imgViewsAlphabet is an array of strings.

alphabet is an array of UIImage objects.

This line:

[(UIImageView *)[imgViewsAlphabet objectAtIndex:index] 
  setImage:[alphabet objectAtIndex:index]];

Is attempting to fetch an object from the imgViewsAlphabet array, and send it a setImage: message.

What kinds of object are in your imgViewsAlphabet array? Do those kind of objects understand the setImage method?

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Hi Duncan, In my code if I comment out the While loop and insert the following line `[letterA setimage:[alphabet objectAtindex:index]];` and run the app the letterA UIImageView in my ViewController populates fine. So the line you hightlighted above, at least in my head, is trying to build this line of code programatically. Should `[(UIImageView *)[imgViewsAlphabet objectAtIndex:index]` in that line not be equivalent to `[letterA `. I'm probably wrong but I though it would and as such I could apply `setimage:[alphabet objectAtIndex:index]];` to it. – Laoistom Jun 09 '14 at 13:45
  • I can't make sense of your comments. (Your original code does not have a variable "letterA") Edit your original question and append your new code to the bottom. (Comments don't allow formatting, so the code is pretty much unreadable.) – Duncan C Jun 09 '14 at 13:54
0

@DuncanC is right. It seems like you're looking to have an array of image views with images chosen in a random, non-repeating way. A good simple approach is to build that array sequentially, then shuffle it:

NSMutableArray *imageViewArray = [NSMutableArray array];
for (char aChar = 'A'; aChar <= 'z'; ++aChar)  {
    NSString *imageName = [NSString stringWithFormat:@"%c.png", aChar];
    UIImage *image = [UIImage imageNamed:imageName];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [imageViewArray addObject:imageView];
}

Now shuffle it. There are many ways to do this, like the Fisher-Yates algorithm. Here's a high voted SO answer that does it. In a nutshell:

NSUInteger count = [imageViewArray count];
for (NSUInteger i = 0; i < count; ++i) {
    NSInteger remainingCount = count - i;
    NSInteger exchangeIndex = i + arc4random_uniform(remainingCount);
    [imageViewArray exchangeObjectAtIndex:i withObjectAtIndex:exchangeIndex];
}

This will leave you with an array of UIImageViews whose images are named @"A.png", @"B.png", etc. in randomized order. You'll need to assign these frames and add them as subviews to some view in order to be seen.

Community
  • 1
  • 1
danh
  • 62,181
  • 10
  • 95
  • 136