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: "Dr. H. Nikolaus Schaller" <email@hidden>
- Date: Sat, 22 Mar 2003 17:51:46 +0100
Ben,
after reading the comments and replys, I have the impression that you
are looking for the following:
1. you have defined a class. The class and its methods are hard coded
2. you want to have several instances (objects) of this specific class,
that are named, i.e. can be accessed by a symbolic name and not by a
compiler variable like in
YourClass *compilervariable=[[[YourClass alloc] init] retain];
In my experience, the best way is to use NSMutableDictionary, which is
an associative container, comparable to arrays in PHP (I have no
experience with Perl).
NSMutableDictionary *dict=[NSMutableDictionary
dictionaryWithCapacity:10];
to add a new instance of your object:
[dict setObject:[[yourClass alloc] init] forKey:yourKey];
yourClass will be replaced by the name of your class or
NSClassFromString(str) if even that has to be variable during runtime.
yourKey is a NSString that is the "name" of the object you are
defining. This string can be generated as you like - read from file,
from user, from network...
Put all this into a loop like
for(i=0; i<15; i++)
[dict setObject:[[yourClass alloc] init] forKey:[NSString
stringWithFormat:@"symbol%d", i]];
To look up the object for a name/symbol:
[dict objectForKey:yourKey];
Note that the NSMutableDictionary keeps care of all retains&releases.
The academic answer to your question may be: Cocoa has no built-in
symbol table for instances.
Hope this helps,
hns
Am Freitag, 21.03.03 um 16:48 Uhr schrieb Ben Dougall:
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.
On Friday, March 21, 2003, at 03:14 pm, email@hidden
wrote:
Why don't you just use an array? or a dictionary if you really have to
give each instance a name...
John.
Ben Dougall <email@hidden>
Sent by: email@hidden
03/21/2003 10:49 AM
To: email@hidden
cc:
Subject: object instance names generated on the fly?
only just starting cocoa/obj-c so this might be very easy to do, or
impossible. i have no idea.
is it possible to get various object instances from one class with
names based/generated from a variable rather than names that have been
typed into the code - so a series of object instances whos names have
been generated by using a for loop for instance?
thanks.
_______________________________________________
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.
_______________________________________________
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.
_______________________________________________
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.
+-------------------+
! email@hidden !
+-------------------+
_______________________________________________
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.