1

I have a int variable called pageIndicator.

I would like to include it inside another variable so it will save me some lines of code.

This is what I have:

Products *productPage0 = [productArray objectAtIndex:0];
Products *productPage1 = [productArray objectAtIndex:1];
Products *productPage2 = [productArray objectAtIndex:2];
Products *productPage3 = [productArray objectAtIndex:3];

And this is what I want:

Products *productPage(@"%d", pageIndicator) = [productArray objectAtIndex:pageIndicator];

Is it possible?

lu yuan
  • 7,207
  • 9
  • 44
  • 78
benklinger
  • 21
  • 3
  • 1
    What's this syntax on the last code line? –  Jul 22 '12 at 06:22
  • 3
    Why can't you use `[productArray objectAtIndex: index]`? – nhahtdh Jul 22 '12 at 06:22
  • 4
    Your design has a major flaw if you need to use dynamic variable names. Tell why do you think you need this, then we can tell you what is the problem with your design/thinking. – Sarwar Erfan Jul 22 '12 at 06:24
  • If its an 'int' you could just make a C array and get the int by using 'productPage[n]'. With 'n' being your indicator. – David Skrundz Jul 22 '12 at 06:27
  • I have a store inside my app, with four different subviews under the same `uiscrollview`. each of them need labels and imageviews get text, image, and some other information. Later I wish to use the pageIndicator integer with ZooZ shopping cart framework to check on what subview the "add to cart" is being used. – benklinger Jul 22 '12 at 06:37
  • possible duplicate of [Objective C Equivalent of PHP's "Variable Variables"](http://stackoverflow.com/questions/2283374/objective-c-equivalent-of-phps-variable-variables) or [Variable as object name](http://stackoverflow.com/questions/7940809/syntax-help-variable-as-object-name) – jscs Jul 22 '12 at 07:24

3 Answers3

1

How'bout a preprocessor macro?

#define PRODUCT_PAGE(n) products *productPage##n = [productArray objectAtIndex:n]

Use it like:

PRODUCT_PAGE(0);
PRODUCT_PAGE(1);

etc.

(This is extremely bad practice, btw).

  • If's interesting. What's the ## syntax for? – lu yuan Jul 22 '12 at 06:27
  • btw, why is a bad practice by using the macro to solve the problem? – lu yuan Jul 22 '12 at 06:43
  • @luyuan because that's not what macros are for. C is in itself a good Turing-complete language in which you should be able to solve problems without tricky metaprogramming. –  Jul 22 '12 at 06:46
  • @luyuan macros' job is 1. to give some convenience by not having to rewrite redundant pieces of code, in a way similar to functions, 2. to define constants which are not to be exposed in the binary code. 3. etc. -- it can't exactly be defined, you'll see it when you have a bit more experience with C. –  Jul 22 '12 at 07:09
0

You can use a dictionary to store your Products object and set the name as the corresponding key.

NSMutableDictionary *dic = [NSMutableDictionary dictionary];

for (int i =0; i < productArray.count; i++) {
    [dic setValue:[productArray objectAtIndex:i] forKey:[@"productPage" stringByAppendingFormat:@"%d",i] ];
}
lu yuan
  • 7,207
  • 9
  • 44
  • 78
0

What is the advantage of having 4 similar named variables against just using your productArray?

Instead of using productPageX you simply could use [productArray objectAtIndex:X].

pre
  • 3,475
  • 2
  • 28
  • 43