• 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: Ben Dougall <email@hidden>
  • Date: Sat, 22 Mar 2003 15:49:26 +0000

On Saturday, March 22, 2003, at 02:26 pm, Sherm Pendley wrote:

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:

no, i'm just expecting (high i guess) dynamic behavior from obj-c. not particularly accustomed to any language apart from c. i do know the sort of thing i want to do with obj-c/cocoa though, and it's a question of finding a way to do that, which i'm sure i will, one way or another.


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.

yes you're right, that's exactly the type of thing i'm after


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];
}

yup, dictionary has cropped up numerous times now, so it really looks like the the thing to look into.


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?

no, not that much - i've been learning c for a while now in order to learn obj-c. beginning to run out of steam with learning c, so have started on obj-c/cocoa - i was putting this off until i'd learnt a good amount of c. i do very much understand and like the conceptual side of oop - that's why i decide to learn obj-c/cocoa.


I ask only because collections of variables - arrays, dictionaries, and such

i know arrays from a c perspective. don't know dictionaries yet. obviously i need to look into dictionaries.

thanks very much for the info.




- 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: Marcel Weiher <email@hidden>
References: 
 >Re: object instance names generated on the fly? (From: Sherm Pendley <email@hidden>)

  • Prev by Date: AECreateAppleEvent
  • Next by Date: Re: object instance names generated on the fly?
  • 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