1

I am having some trouble finding help online which makes me think what I am trying to do may not be possible, but I'll ask anyway! I am new to obj-c and I am only developing for a bit of fun...

I am trying to call an array based on a string pulled at random from another array. I have an NSArray called pickerArray which holds the strings: oneArray, twoArray as an example.

I also have NSString (pickerString), NSArray (oneArray) and NSArray (twoArray).

I can randomly pick the string fine, what i can't do is use the values of this sting as 'code'

randomNumber = arc4random()%[pickerString count]; 

pickerString should read as oneArray or twoArray in the code depending on which string
is picked, i.e.

randomNumber = arc4random()%[oneArray count]; 

or:

randomNumber = arc4random()%[twoArray count]; 

depending on the string picked.

Edit: My current code is:

@implementation FirstViewController{

IBOutlet UITextView *doThis;
}
- (IBAction)takeATurn:(id)sender {
int pickerNumber;
int randomNumber;
NSString *pickerString;
NSString *textString;
NSArray *pickerArray;
NSArray *oneArray;
NSArray *twoArray;

pickerArray = [NSArray arrayWithObjects:
               @"oneArray",
               @"twoArray",

               nil];

oneArray = [NSArray arrayWithObjects:
               @"example text to display",
               @"expmple text to display 2",

               nil];
twoArray = [NSArray arrayWithObjects:
               @"example text to display in array two",
               @"expmple text to display in array two 2",

               nil];

pickerNumber = arc4random()%[pickerArray count];

pickerString = [pickerArray objectAtIndex:pickerNumber];

randomNumber = arc4random()%[NSArray(pickerString) count];
     //does not compile - should read [oneArray count]; (or twoArray in place of oneArray

textString = [NSArray(pickerString) objectAtIndex:randomNumber];
     //same as above.


doThis.text = textString;
}

Thanks for any help. Mike

Mike
  • 13
  • 3

2 Answers2

2

This is a key-value coding problem:

NSArray *arrayToUse = [self valueForKey:pickerString];

This will return you the property named by pickerString.

To make this work the way you would like, move the arrays into instance properties rather than local variables.


EDIT:

It would be something like this:

@interface FirstViewController ()
@property (nonatomic, readwrite, weak) IBOutlet UITextView *doThis;

@property (nonatomic, readwrite, strong) NSArray *pickerArray;
@property (nonatomic, readwrite, strong) NSArray *oneArray;
@property (nonatomic, readwrite, strong) NSArray *twoArray;
@end

@implementation FirstViewController

// You can put this wherever you initialize the object (init, viewDidLoad, awakeFromNib, etc.)
// viewWillAppear just tends to work for a wide variety of design choices.
- (void)viewWillAppear:(BOOL)animated {
  self.pickerArray = @[
                       @"oneArray",
                       @"twoArray",
                     ];

  self.oneArray = @[
                    @"example text to display",
                    @"expmple text to display 2",
                  ];

  self.twoArray = @[
                    @"example text to display in array two",
                    @"expmple text to display in array two 2",
                  ];
}

- (IBAction)takeATurn:(id)sender {
  int pickerNumber = arc4random_uniform([self.pickerArray count]);
  NSString *pickerString = pickerArray[pickerNumber];
  NSArray *array = [self valueForKey:pickerString];
  int randomNumber = arc4random_uniform(array count]);
  NSString *textString = array[randomNumber];
  self.doThis.text = textString;
}
Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Thank you very much for your prompt help! Much appreciated, as I said I am just trying to learn for a bit of fun, for my day job I control an oil rig in the north sea and at times there can be very little to do! – Mike Dec 22 '13 at 17:04
  • I have probably been awake too long, but I'm still struggling with this! – Mike Dec 22 '13 at 22:21
  • Thanks, another massive help there, I am having some issues with int randomNumber = arc4random_uniform(array count]) NSString textString = array[randomNumber]; not compiling (aside from missing brackets and semi colon). An identifier is expected and interface type cannot be statically allocated are shown. I'll have another go in the morning :) – Mike Dec 22 '13 at 23:00
  • Thanks for your input, I now have it working and I believe I understand it too! ;) Thanks again, Mike – Mike Dec 23 '13 at 06:31
0

You can put the code blocks into methods and try the solution posed here:

https://stackoverflow.com/a/4446914/584663

Community
  • 1
  • 1
Bill
  • 884
  • 6
  • 23
  • Thanks, I did read this post yesterday but I was not too sure how i could use it, I was trying to alter it to suit I think by using NSArrayFromString. – Mike Dec 22 '13 at 16:59