I’m creating draggable letter tiles for all the letters in the alphabet. I’m creating 5 of each letter in the case that a word requires more than one of the same letter.
I’m programmatically creating each UIImageView in my MainViewControllers ViewDidLoad: method.
- (void)ViewDidLoad:
{
for (int i = 1; i <= 5; i++)
{
letterA = [[LetterTiles alloc] initWithFrame:CGRectMake(39, 104, 70, 70)];
[letterA setImage:[UIImage imageNamed:@"a.png"]];
letterA.tag = i;
[letterA setUserInteractionEnabled:YES];
[self.view addSubview:letterA];
[letterA release];
for (int i = 1; i <= 5; i++)
{
letterB = [[LetterTiles alloc] initWithFrame:CGRectMake(112, 104, 70, 70)];
[letterB setImage:[UIImage imageNamed:@"b.png"]];
letterB.tag = i;
[letterB setUserInteractionEnabled:YES];
[self.view addSubview:letterB];
[letterB release];
}
for (int i = 1; i <= 5; i++)
{
letterC = [[LetterTiles alloc] initWithFrame:CGRectMake(185, 104, 70, 70)];
[letterC setImage:[UIImage imageNamed:@"c.png"]];
letterC.tag = i;
[letterC setUserInteractionEnabled:YES];
[self.view addSubview:letterC];
[letterC release];
}
}
I have a few questions:
-
Should I be creating an array for each letter, or creating a single array for all the letters?
-
What kind of array should I be using? NSMutableArray/NSArray or NSMutableDictionary/NSDictionary? -I have been trying to figure out how to add all these letters to all the above array types, then retrieve them based off their letter and tag, but cannot for the life of me figure it out. Would someone be willing to show me an example with one or two of the letters in code?
-
Is there a better way to do this than what I’m showing and asking here?
Question 1
Having a single NSArray for all the letters is better, it reduces the amount of variables you’ll have to deal with. Having something like this:
Is just a lot of code for something that can be made readily accessible in a single data structure, not to mention, easier to update if you plan on adding numbers, puncutation, etc.
Question 2
Mutable means that it can be updated, so the important question, should your Array/Dictionary update on runtime? If the answer is no, a non-mutable array might be better. On the other hand, you’ll have to automatically fill the array with a lot of stuff, so having a mutable version may allow you to have cleaner code (Instead of having a
[[NSArray alloc] initWithObjects:[UIImage imageNamed:@"A"], ...]).So I would go with the mutable version, just for cleaner code.
Question 3
This is a personal choice, but I’m not a big fan of having nested data structures in statically typed languages (Doing this in python is a bliss, for instance
myDictArray["key"][1]). I would use a Mutable Dictionary, where the keys are composed, to store the pertinent data:On the other hand, instead of using images, I would use big square UILabels, to save up in memory (and mental sanity, dealing with that many images, ugh), and pick one of the many fonts that iOS uses (Here’s a complete list).
With this, generating everything, would require two nested fors, one to iterate through each letter, and then one for each 5 repetitions, and then create the label and throw it inside the dictionary (I would write code, but I’m not on a mac).