PruitIgoe
PruitIgoe

Reputation: 6384

Objective-c: Dynamic Class Names

I'm not sure if I worded the subject correctly. I am looping through an array, within each loop I am trying to instantiate a class, but I want to dynamically create the name. Like so:

int i = 0;

for(NSString* thisdatarow in filedata) { 
    i++;
    NSString* thisad = [NSString stringWithFormat:@"ad%d", i];
    NSLog(@"%@", thisad);
    AdData* thisad = [AdData new];  
}

In the example above I want AdData* thisad... to be named dynamically - "ad1", "ad2", "ad3"...and so on. I get a conflicting type error.

This code also generated an error:

int i = 0;
for(NSString* thisdatarow in filedata) { 
    i++;
    AdData* [NSString stringWithFormat:@"ad%d", i] = [AdData new];
}

Is there a way to do this?

Upvotes: 0

Views: 782

Answers (4)

newacct
newacct

Reputation: 122519

Local variable names are a purely compile-time concept. So you cannot do anything "dynamic" (i.e. at runtime) with it. The compiler is free to rename the variables and add or remove variables as it sees fit.

If you think about it, what is the point of dynamically manipulating local variable names? In order to use the dynamically-named variable again, you must either 1) explicitly refer to the variable name, in which case you have hard-coded the name (not so dynamic), or 2) dynamically construct the name again. If it's (1), then there is only a fixed set of variable names, so dynamic-ness is unnecessary. If it's (2), you're missing the point of local variable names (the whole point of which is so they can be referred to explicitly).

Upvotes: 0

Jasarien
Jasarien

Reputation: 58468

This isn't possible. While Objective-C is very dynamic, it's not that dynamic.

The suggested way to do this would be to create your instances and put them into an array, not assigning them to explicitly named variables.

You can then refer to them individually using their index in the array.

Something like this:

NSMutableArray *ads = [NSMutableArray array];

for(NSString* thisdatarow in filedata) { 
    AdData* thisad = [[[AdData alloc] init] autorelease];  
    [ads addObject:thisad];
}

// get third ad:
AdData *ad = [ads objectAtIndex:2];

Alternatively you could create an NSDictionary, if you really want to refer to them by a name, like this:

NSMutableDictionary *ads = [NSMutableDictionary dictionary];
int i = 0;

for(NSString* thisdatarow in filedata) { 
    i++;
    AdData* thisad = [[[AdData alloc] init] autorelease];
    NSString *keyName = [NSString stringWithFormat:@"ad%d", i];
    [ads setObject:thisad forKey:keyName];
}

// get third ad
AdData *ad = [ads objectForKey:@"ad2"];

Upvotes: 2

nosirrahcd
nosirrahcd

Reputation: 1467

You can't do that in Objective-C.

Use a NSString to AdData map--it'll do basically the same thing!

**edit: To clarify, use an:

NSMutableDictionary *dict;

with keys that are NSString* objects containing the ad names, and values that are the AdData* objects.

i.e.

[dict setValue:ad1 forKey:@"ad1"];

to set the values, and

[dict valueForKey:@"ad1"];

to get the values. (ignore the obvious memory leaks there with the strings...)

Upvotes: 2

Richard J. Ross III
Richard J. Ross III

Reputation: 55583

Cant be done Without using a C array, which would look like this:

AdData **ad = malloc(sizeof(AdData) * numberOfAds);
ad[1] = [AdData new];
// etc.
if (ad)
    free(ad);

But I don't know how that would work because of how Objective-C classes are stored....

Upvotes: 0

Related Questions