swarm-support
[Top][All Lists]
Advanced

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

Re: Casting an id type to a specific object type?


From: Ken Cline
Subject: Re: Casting an id type to a specific object type?
Date: Fri, 14 Mar 1997 09:30:11 -0500 (EST)

Jae,

Not to speak out of turn, but here are a few things to
try...

DefObjects and its subclasses have a method called
"respondsTo:" defined for them.  This may provide a way to
trap the error and prevent the core dump.  For example,

   if ( [tgt respondsTo: M(isItAlive)] )
      status = [tgt isItAlive];
   else
      fprintf(stderr,"tgt can't determine if it is"
                     "Alive!\n");

Other possibilities include trying to figure out exactly
what type of class "tgt" is:

   fprintf(stderr, "tgt thinks it is a %s?\n", 
          [ [tgt getClass] getName ] );

or

   if ( [tgt isMemberOfClassNamed: "Person"] )
        fprintf(stderr,"tgt is a 'Person'!\n");
   else
        fprintf(stderr,"tgt is a nobody!\n");


You might call this the "Looks like a duck, quacks like a
duck,..." testing. :)

Of course I've only helped you rule out certain
possibilities... I'm sure Glen will be able to give you
more (I just love shift responsibility:).

Oh yeah, you could also try explicit casting, that is:

    status = (bool)[ ((Person *)tgt) isItAlive];

Good luck,

Ken.



On Fri, 14 Mar 1997, Jae Chan Oh wrote:

> Hi Glen,
> This question is in the same thread as the second question of my
> last email about updateEnergy method in my program.
> 
> Say, in a method,
> I have a delcation:
> 
> -myMethd {
> (id) tgt;
> bool status;
> 
>     if (((tgt = [world getObjectAtX: x1 Y: y1]) != nil))
>       status = [tgt isItAlive];
> 
> //   some stuff .......
> 
> }
> 
> Let's say "isItAlive" is a method of "Person" type. I get core dump
> at "status = [tgt isItAlive]" line since objc_msg_lookup
> can't find the method. I have tried to declare tgt as pointer to
> Person i.e.: Person * tgt. And tried many other things but no luck.
> 
> How do I cast tgt to Person type so it can recognize the method "isItAlive"?
> Should using protocol would help? 
> i.e. declaring tgt as "(id <PersonProtocol>)" tgt would help?
> I'd like to avoid delcaring protocol if possible.
> 
> FYI, the following is gdb error messages after the crash:
> (gdb) where
> #0  objc_msg_lookup (receiver=0x368b00, op=0x278d10) at sarray.h:216
> #1  0x41c2c in _i_Person__look (self=0x368880, _cmd=0x278b48) at Person.m:353
> #2  0x404e0 in _i_Person__step (self=0x368880, _cmd=0x27934c) at Person.m:50
> 
> 
> Thank you,
> 
> -Jae
> 

_________________________________________________________
Ken Cline                             address@hidden
SAIC                                 VOICE (410) 571-0413
Annapolis, MD                          FAX (301) 261-8427




reply via email to

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