• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: object instance names generated on the fly?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: object instance names generated on the fly?


  • Subject: Re: object instance names generated on the fly?
  • From: Sherm Pendley <email@hidden>
  • Date: Sat, 22 Mar 2003 09:26:30 -0500

On Saturday, March 22, 2003, at 08:43 AM, Ben Dougall wrote:

no i am not thinking of something else. my point is that the number of and the names of the object instances that i wish to use during runtime is unknown at compilation. hence why i'm asking for object instance variable names.

It sounds to me like you may be accustomed to Perl, or some other dynamic interpreted language. In Perl, for example, you could do something like this:

for(my $i=0; $i < 10; $i++) {
${'myobject' . $i} = new Foo;
}

The result would be ten instances of Foo, named $myobject0, $myobject1, etc.

This sort of thing is possible in Perl because variable names are accessible at runtime. In fact, the entire name space is implemented as a series of nested hash tables, which can be manipulated at run time. If you iterate over the keys of the hash named "%Foo::", for example, you'll get a list of all the names in package Foo.

Objective-C, like C, is much stricter about that sort of thing. Variable names are only used at compile time. When the code is compiled into a binary, the names of variables are stripped away and replaced with the actual location of the data in memory.

So, the closest you can get in Objective-C to the Perl example above is something like this:

NSMutableDictionary *myObjects;
NSString *objectName;
int i;

myObjects = [[NSMutableDictionary alloc] initWithCapacity: 10];
for(i=0; i<10; i++) {
objectName = [NSString stringWithFormat: @"myobject%d", i];
[myObjects setObject: [[Foo alloc] init] forKey: objectName];
}

You could then access your Foo objects by the names you've given them with something like this:

Foo *anObject = [myObjects objectForKey: @"myobject1"];

Having said all that, I'd like to ask a question of you. I mean no disrespect by it, so please don't be offended. How much programming experience do you have? I ask only because collections of variables - arrays, dictionaries, and such - are a fairly fundamental concept. Beginners often struggle with the idea, just as you seem to be doing. It would seem to me that you're either new to imperative languages such as C, or new to programming altogether. In either case, I think that a good introductory class or book on C would be a great deal of help.

sherm--
_______________________________________________
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.

  • Follow-Ups:
    • Re: object instance names generated on the fly?
      • From: Ben Dougall <email@hidden>
References: 
 >Re: object instance names generated on the fly? (From: Ben Dougall <email@hidden>)

  • Prev by Date: creating new instances
  • Next by Date: RE:NSOpenGL
  • Previous by thread: Re: object instance names generated on the fly?
  • Next by thread: Re: object instance names generated on the fly?
  • Index(es):
    • Date
    • Thread