PDA

View Full Version : Using string as #import variable




Rendse
Oct 25, 2010, 05:36 AM
Hi all,

quick objective-c question:

I have a list of header files. I want to import one of these header files based on a string variable:
exampleString = "example.h"

but '#import exampleString' doesn't work

How do I convert the string in order to let it be recognized as a variable name?

Thanks



PhoneyDeveloper
Oct 25, 2010, 09:47 AM
You can't do that directly. You can conditionalize your imports with macros.

#if SOMECONDITION
#import "x.h"
#else
#import "y.h"
#endif

dejo
Oct 25, 2010, 10:35 AM
My question is: why do you think you need to do this?

Rendse
Oct 26, 2010, 03:51 PM
Thank you for the replies.

I asked the same question on another forum, and I got the answer that my method isn't possible since #import is interpreted at compilation time, while strings are interpreted later, at run-time. Thus the compilation error. It seems that using #import is taking the wrong approach.

I have a bunch of header files, each containing arrays with 3d vertex points and UV texture coordinates (3d models as OpenGL code, created with Obj2OpenGL if you are familiar with it). But the program only need to use the data from one model at the time - so what would be the best approach, considering the program preferably should run sowewhat efficiently?

PhoneyDeveloper
Oct 26, 2010, 04:25 PM
What do you mean by "the program only need to use the data from one model at the time"?

If what you want is to #import certain header files and to determine this at compile time then you can use a macro, like I showed above. #define the macro either in your pch file or for the -D in your build settings. Then surround the #includes like I showed earlier.

In effect this is the same as commenting out certain includes. This is also the way that one does DEBUG versions of apps.

chown33
Oct 26, 2010, 04:47 PM
I have a bunch of header files, each containing arrays with 3d vertex points and UV texture coordinates (3d models as OpenGL code, created with Obj2OpenGL if you are familiar with it). But the program only need to use the data from one model at the time - so what would be the best approach, considering the program preferably should run sowewhat efficiently?

How many is "a bunch"? Dozens? Hundreds? Thousands?

When the largest of these files is compiled, how big is the resulting compiled code and data? Kilobytes? Hundreds of kilobytes? Megabytes?

How efficient does it have to be in order to qualify as "somewhat" efficient?

I'm asking all of this because you're trying to be efficient without having quantified the problem, nor quantified what would be an acceptable solution of the problem.

My first thought is that you should just import all the headers all at once, and not care at all about the size. Unless you have some measured numbers and a problem with size or performance, you're worrying about something that isn't even perceivable yet. Or if it's perceivable you haven't said what the problem is.

My second thought is that compiling models as source isn't a plan that lends itself to long-term or large-scale efficiency. There is a reason that models are often kept as data files to be read when a program runs. Even there, though, one doesn't use the same strategies for programs with dozens of models vs. those with thousands or millions of models.


The question you've asked is how to get your current solution to work, instead of how to solve the actual underlying problem. You should outline the problem you're really trying to solve by using Obj2OpenGL. If it's "load OBJ models and use them in OpenGL", that's a problem that might be solved in many different ways. You've chosen a solution that might not be the best one, but without knowing what the problem really is and its scale, it's difficult to offer advice.