swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Can I ask a Objective-C Class Question, please?


From: Paul E. Johnson
Subject: Can I ask a Objective-C Class Question, please?
Date: Wed, 01 Aug 2001 15:10:09 -0500

Studying the Java docs on reflection made me curious to know about
Objective-C reflection.  This code example I describe here works, but I
don't understand why. I arrived at it by rooting around a long time.

Suppose you want a class (my example is MultiGrid2d)  to have a method
that take's another class as an argument, and then builds instances of
the input class whenever necessary.  There is an IVAR in the MultiGrid2d
class:

Class cellClass

And then a setter method:

-(void) setCellClass: (Class) aClass {
  cellClass = aClass;
}

I have defined a protocol AppCell that all cell classes must implement,
so the compiler does not complain to me.  As a result, this approach
works to create a cell of the right kind, whenever the MultiGrid2d needs
a cell:

-createCell
{
    id <AppCell> cell;

     cell = [cellClass createBegin: [self getZone]];
     cell = [cell createEnd];
    }
   return cell;
}


Now here is the part I don't understand.  What in Swarm ought a person
do to create a Class argument to give to the setCellClass method?  I
tried lots of ways to create arguments and dug through the objc Object.h
header, and then stumbled on Swarm's own getClass method, which works,
but I don't see why.  My cell class in this example is PaulCell.  To get
a Class object, I create an instance of PaulCell, and then pass through
the return from getClass:

    PaulCell * cell = [PaulCell createBegin: self];
      cell = [cell createEnd];

      homeGrid = [ MultiGrid2d create: self setSizeX: worldXSize Y:
worldYSize];
      [homeGrid setCellClass: [cell getClass] ];


This works, but I wonder why. I had tried to use the Objective-C method
"class" to do the same, but it did not work because there was a runtime
error indicating that PaulCell did not implement +createBegin.  But
SwarmObject's getClass method does.  Is there a way to get the Class
without creating an instance from which to getClass?

Wishing Objective-C had so many how to books as java!
Paul Johnson


                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]