iphone - variable - python title number

Is it possible to reference a variable with a string and an int? (5)

Is it possible to reference a variable with a string and an int, like this:

    int number1;

    int j = 1;

    @"number%i", j = 3; //Hope this makes sense..

The code above gives me warnings and does not work, how could this be done.

I also tried this, but it doesnt work (for quite obvious reasons):

    int j = 1;

    NSString *refString = [NSString stringWithFormat:@"number%i", j];

    refString = 3;

Im really struggling with this, I know how to do it in Javascript, but not in Obj-C, is it possible?

From what I can infer you are trying to set/retrieve different variables based on the value of j.

You could use a dictionary for this purpose:

NSMutableDictionary *numbers = [NSMutableDictionary dictionary];
int j = 1;
[numbers setObject:[NSNumber numberWithInt:3] forKey:[NSNumber numberWithInt:j]];

And then to retrieve:

[[numbers objectForKey:[NSNumber numberWithInt:j]] intValue];

It's a bit verbose, but you could simplify it by creating a small class.

If the arrays are properties of some object (your view or view controller) you can use KVC to get it:

NSArray *myArray = [self valueForKey:myString];

But this is really bad practice. Why don't you put all your buttons into an array instead. Put your NSMutableArrays in a second array of same size. Then you can refer to them by index.

In your header:

@property (nonatomic) NSArray *buttons;
@property (nonatomic) NSArray *arrays;

In your implementation, after creating the buttons or loading them from a nib file (so this should probably go into either viewDidLoad or awakeFromNib):

self.button = @[button1, button2, button3 ... ];
self.arrays = @[array1, array2, array3 ... ];

Then, when the user taps a button:

- (IBAction)buttonTapper:(id)sender {
    UIButton *button = (UIButton *)sender;
    NSUInteger index = [self.buttons indexOfObject:button];
    NSMutableArray *theArray = [self.arrays objectAtIndex:index];
    // do something with the selected array.

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


Access an array according to a NSString's value

Create a dictionary that holds the arrays. The dictionary key will be the title you're looking for.

For instance, insert button1's array to the dictionary, when you want to retrieve the count use:

NSInteger count = [myDictionary[@"Comp1"] count];

Use value of variable as dynamic code in objective c?

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.


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;

@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 = @[

  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;