0

I have the following code:

 NSInteger variableScene = 10;
 NSInteger numberOfRowsXX = //an Integer value; <-- I would like replace XX with the value of 'variableScene'

So it'll look like this:

NSInteger numberOfRows10 == //an Integer value;

How can I replace XX with the value of variableScene?

*Update*

Why I'm trying accomplish this?

It's a bit complicated. I'm using Core Data with remote Database. In a nutshell: I have 10 Scenes that'll be presented based on the User selection order. For each Scene I need to assign the numberOfRows to present at the end of all the Scenes with a UITableViewController.

I have a variableScene that I pass around from Scene to Scene.

I need to assign numberOfRowsSceneXX = //an integer

XX will come from variableScene, which could be any number from 1 - 10.

So when we get to the last scene, we'll have a value for each Section (numberOfRowInSection) which will be represented by each Scene: numberOfRowScene1, numberOfRowsScene2, etc.

I tried to simplify the question. Hope it makes sense.

5
  • What is the use case for this? You can concatenate tokens with macros #define SECTION_ROW(name) sectionRow ## name but I'd be interested to know what you're trying to accomplish. Commented Sep 3, 2013 at 3:18
  • 8
    You almost certainly want an array of NSIntegers here. What are you trying to do? This looks like you're trying to solve a problem "the hard way". Commented Sep 3, 2013 at 3:22
  • According to your update, it's still not persuading why an array can't be used. You can have a global array (or at least an array where all the Scenes can access to update). For every scene, you update numberOfRowsInScene[i], where i = variableScene-1. Unless it's not what you are up to. Commented Sep 3, 2013 at 3:46
  • 1
    Use an NSArray. Once compiled, local and (c-)global variables only exist as memory addresses or even just registers; names are only a way to make it easier for you to keep straight what is what. Technically, it is possible to do this with properties, but it's a really bad idea. Commented Sep 3, 2013 at 3:57
  • Got it. Makes sense. Thanks. If someone wants to answer it, I'll mark it as an acceptable answer. Commented Sep 3, 2013 at 4:01

1 Answer 1

2

Use arrays.

set it up as:

#define MAX_SCENES 10
NSInteger numberOfRows[MAX_SCENES];

then when you want to set the value:

if (variableScene < MAX_SCENES) {
  numberOfRows[variableScene] = variable;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.