RE: object instance names generated on the fly?
RE: object instance names generated on the fly?
- Subject: RE: object instance names generated on the fly?
- From: "Jonathan E. Jackel" <email@hidden>
- Date: Fri, 21 Mar 2003 11:41:02 -0500
>
thanks for the reply.
>
>
because an array will behave in a very different way to my object. i
>
want to initiate this object many times, so i guess the precise
>
question is: is it possible to initiate an object with a name that's a
>
variable? rather than what seems to be the norm - with a name that
>
you've typed in the code.
>
I think the answer is no, but I'm having a bit of trouble understanding what
you are after. You'll get a better answer if the question is, can I access
an instance of my object based on something that the program will provide at
runtime? The answer to that one is yes.
As John said, you can instantiate as many objects as you want and put them
in an array. When you want a particular one, you can get to it by index
number, e.g.
int x;
NSMutableArray *things = [NSMutableArray array];
for(x=0; x<1000;x++)
{
YourObject *you = [[[YourObject alloc] init] autorelease];
[things addObject:you];
}
YourObject *theFiftiethThing = [things objectAtIndex:50];
Or, you can put your objects in a dictionary with keys that you create
programmatically, like so:
int x;
NSMutableDictionary *things = [NSMutableDictionary dictionary];
for(x=0; x<1000;x++)
{
YourObject *you = [[[YourObject alloc] init] autorelease];
NSString *key = [NSString stringWithFormat:@"Object Number %d", x];
[things setObject:you forKey:key];
}
YourObject *theFiftiethThing = [things objectForKey:@"Object Number 50"];
The idea here is that the key string could be anything: text from a text
field, the result of a calculation, etc.
If neither of these suits your needs, maybe you can explain why.
Jonathan
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.