-1

I have values i wanted to load dynamically in an array, here are the examples

First of all i define 3 different values in my init, and then in my array i want it to determine which value to read. Example: First i define the value

int value1=20
int value2=40;
int value3=60;

i then define another int in my array called valueToLoad, and i'll give each of them a number tag. and i want the individual array item to read different value based on their number tag so that Item 1 will read value1, Item 2 will read value2 and so on. i tried the method below to convert NSString into int:

NSString *valueVariable=[NSString stringWithFormat:@"value%d",i]; (i being the number tag)
int valueToRead = [valueVariable intValue];

unfortunately, this conversion doesn't supports conversion of any other thing except if the string is actual integer. However i do not want to run the IF statement to do:

if(tag==1)
{ int valueToLoad= value1;}

For who don't understand. I am just trying to read different Int value in an array based on the number of array. Let's assume i have 3 Items in array naming A,B,and C. i want Item A to read Value 1, ItemB to read Value2 and so on.

Bek
  • 31
  • 1
  • 6
  • Your question is difficult to understand. Can you please try to explain why you're trying to do what you're doing? – Andrew Madsen Mar 07 '12 at 17:35
  • 1
    Have you tried NSDictionary. I don't what's your purpose, but it seems NSDictionary is more appropriate for waht you are doing. – Sierra Alpha Mar 07 '12 at 17:37
  • Uhmm... Just trying to read different Int value in an array based on the number of array. Let's assume i have 3 Items in array naming A,B,and C. i want Item A to read Value 1, ItemB to read Value2 and so on. – Bek Mar 08 '12 at 02:15

3 Answers3

2

Why don't you simply do something like

int values[] = {20,40,60};

...

int valueToRead = values[i]; //or i-1, depending if i starts from 0 or 1

?

Manlio
  • 10,768
  • 9
  • 50
  • 79
0

Not sure about the context of your problem but why don't you use an NSDictionary? That way you can store your number tag as the key and your value to read as the value...

You fill your dictionary like this:

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:value1, tag1, value2, tag2, nil];

Read your value using : [dict objectForKey:tag1];

Manlio
  • 10,768
  • 9
  • 50
  • 79
AkademiksQc
  • 677
  • 1
  • 6
  • 20
0
  • You can use an array, store your variables in there, and index into it.
  • You can use a dictionary, store your variables as values, and look them up by key.
  • You can declare all your variables as @property, and then use [self valueForKey:] to look them up by name.
  • You can build the name of the ivar as a string, and then use something like object_getInstanceVariable() to retrieve it's value directly (this is similar to #3, except you don't have to declare it as an @property).
  • If you're dealing with views, you can assign each view a unique tag and then retrieve it via [superview viewWithTag:aTag]. EDIT: Note that this only works with instance variables. This does not work with global/static variables.

took from: Objective C Equivalent of PHP's "Variable Variables"

Community
  • 1
  • 1
Daniel
  • 1,321
  • 12
  • 25