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

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.

  • App not installed…because its resources have been modified
  • iPhone 6.1 simulator screen resolution is 320x548 instead of 640x1136, why does it heppend?
  • Disable/Enable back button from detail view
  • iOS 9 Facebook SDK Login/Share
  • How do I enter RGB values into Interface Builder?
  • Prevent iPhone app to run on iPad
  • 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?

    2 Solutions Collect From Internet About “Is it possible to reference a variable with a string and an int?”

    This is an anti-pattern I call the Poor Man’s Array. The better way to do this is to use a proper collection like an array instead of a bunch of variables that are secretly related. Done right, the code with an array will usually be a lot shorter and cleaner too.

    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.