1

I'm just delving into Objective C and Cocoa Touch and I'm trying to build an app for personal use.

My goal is to create an app that displays a randomized NSString in a center window on the iPhone screen while also displaying a scrollable list of associated NSStrings in another window on the side of the screen.

For example: if the center NSString is the name of an animal, such as "Lion", the NSStrings on the list next to it would also be animals (e.g., "Tiger", Snow Leopard", etc.)

I would like to create "packages" of associated NSStrings, have the program randomly select a "package", randomly display one of its NSStrings in the center, and simultaneously display a list of the other NSStrings in the package in the scrollable side window. After a given time interval the program would then loop and select another NSString, excluding those in the previously displayed "package".

My main interest is how to create such "packages" of NSStrings. Is it feasible to use NSDictionary or NSArray to create them?

Since I am just starting out I am hoping that someone can point me in the right direction in my research so that I know what tools I should use to begin experimenting.

I'd very much appreciate any recommendations or example code.

Thanks!

Orpheus Mercury
  • 1,617
  • 2
  • 15
  • 30

1 Answers1

1

Use both NSArray and NSDictionary for it.

For example:

NSArray *animalArray = [NSArray arrayWithObjects:@"cat", @"dog", ... , nil];
NSArray *drinkArray = [NSArray arrayWithObjects:@"coke, @"tea", ... , nil];
...

NSMutableDictionary *wordsDictionary = [[NSMutableDictionary alloc] init];
[wordsDictionary addObject:animalArray forKey:@"animal"];
[wordsDictionary addObject:drinkArray forKey:@"drink"];
...

And you can get all keys using [NSDictionary allKeys]

Hanon
  • 3,917
  • 2
  • 25
  • 29
  • thank you very much for the code @Hanon! I have a question about randomization: Would I be able to use [NSDictionary allKeys] to randomly access keys to display? – Orpheus Mercury Feb 21 '12 at 03:36
  • Yes. `[[wordsDictionary allKeys] objectAtIndex:arc4rand()%[wordsDictionary count]]` – Tim Feb 21 '12 at 03:37
  • I've got one more follow up question. With your help I've been able to create NSArrays of word lists, "package" them in an NSMutableDictionary, and access keys randomly. So I can display the randomly accessed key, but I haven't yet figured out how to display the NSStrings contained within the NSArrays after accessing a key. Any pointers @Tim, @Hanon? – Orpheus Mercury Feb 22 '12 at 00:09
  • I experimented with ObjectForKey: and that gave me the list output of the NSArray objects packaged in the NSDictionary that I wanted, but I had to specify a particular key, and couldn't used allKeys to randomize the output. So I'm wondering if there is anything that I haven't yet come across that is like ObjectForKey: that won't make me specify one key at a time.. – Orpheus Mercury Feb 22 '12 at 00:18
  • Do you mean you want to pick up a random string from the list? – Hanon Feb 22 '12 at 02:20