Main Page | Report this Page
Computers Forum Index  »  Computer Languages (Objective-C)  »  Obtaining actual argument and return type at runtime...
Page 1 of 1    

Obtaining actual argument and return type at runtime...

Author Message
Emmanuel Stapf [ES]...
Posted: Sun Jul 05, 2009 8:21 pm
Guest
Hi,

Is there a way to query at runtime the actual argument type or return
type of a method? Currently I'm using `method_getArgumentType' and
`method_getReturnType' but when the type is an objective-C class, I only
get ` at (no spam) ' which is not sufficient for my purpose. Ideally I'd like to have
the type name (the same way you get the structure name when it is a struct).

Any idea on how to do that?

Thanks,
Manu
 
hns at (no spam) computer.org...
Posted: Sun Jul 05, 2009 8:21 pm
Guest
On 5 Jul., 20:14, p... at (no spam) informatimago.com (Pascal J. Bourguignon)
wrote:
Quote:
"Emmanuel Stapf [ES]" <manus_no_s... at (no spam) eiffel.nospam.com> writes:

Hi,

Is there a way to query at runtime the actual argument type or return
type of a method? Currently I'm using `method_getArgumentType' and
method_getReturnType' but when the type is an objective-C class, I
only get ` at (no spam) ' which is not sufficient for my purpose. Ideally I'd like
to have the type name (the same way you get the structure name when it
is a struct).

Any idea on how to do that?

AFAIK, even when you declare a more precise type, you may get at run
time an object of any type.  The difference is that the compiler may
check at compilation time that the type of the parameter is a subclass
of the type of the parameter, and that the message sents to the
parameters are understood by the parameter declared class (but notice
how if it not the case for this later case, you only get a warning).

That is, these parameter type declarations are only used at
compilation time to make some checks strictier.

You may still set things up at run-time to pass any kind of object, as
long as they are able to handle the messages sent to them.

So, AFAIK, that would be the reason why you get nothing more precise
than ' at (no spam) ' at run-time for the type of these parameters and returns.

--
__Pascal Bourguignon__


If I understand you correctly you have

- (MyClass *) method:

and you get an ' at (no spam) ' when you ask for the return type but you want to
have "MyClass".

Since Obj-C does not completely enforce the return type (the method
can return *any* object incl. nil), you can't get this information by
inspecting method signatures.

Simply call the method and inspect the class (name) of the returned
object.

-- hns
 
Pascal J. Bourguignon...
Posted: Sun Jul 05, 2009 10:14 pm
Guest
"Emmanuel Stapf [ES]" <manus_no_spam at (no spam) eiffel.nospam.com> writes:

Quote:
Hi,

Is there a way to query at runtime the actual argument type or return
type of a method? Currently I'm using `method_getArgumentType' and
method_getReturnType' but when the type is an objective-C class, I
only get ` at (no spam) ' which is not sufficient for my purpose. Ideally I'd like
to have the type name (the same way you get the structure name when it
is a struct).

Any idea on how to do that?

AFAIK, even when you declare a more precise type, you may get at run
time an object of any type. The difference is that the compiler may
check at compilation time that the type of the parameter is a subclass
of the type of the parameter, and that the message sents to the
parameters are understood by the parameter declared class (but notice
how if it not the case for this later case, you only get a warning).

That is, these parameter type declarations are only used at
compilation time to make some checks strictier.

You may still set things up at run-time to pass any kind of object, as
long as they are able to handle the messages sent to them.


So, AFAIK, that would be the reason why you get nothing more precise
than ' at (no spam) ' at run-time for the type of these parameters and returns.

--
__Pascal Bourguignon__
 
spikeysnack...
Posted: Mon Jul 06, 2009 9:08 pm
Guest
On Jul 5, 9:21 am, "Emmanuel Stapf [ES]"
<manus_no_s... at (no spam) eiffel.nospam.com> wrote:
Quote:
Hi,

Is there a way to query at runtime the actual argument type or return
type of a method? Currently I'm using `method_getArgumentType' and
`method_getReturnType' but when the type is an objective-C class, I only
get ` at (no spam) ' which is not sufficient for my purpose. Ideally I'd like to have
the type name (the same way you get the structure name when it is a struct).

Any idea on how to do that?

Thanks,
Manu

The introspection built into the runtime Should do for you.
[object class] returns a class object (not an instance) of objects
class
[object name] returns the class name of the object
[object description] returns a descriptive string of objects class

[object isMemberOfClass: [foo class] ] returns YES if object is a foo
object
[object isKindOfClass: [foo class] ] returns YES if object is a foo
object or subclass of foo
[object respondsToSelector: at (no spam) selector(bar)] YES if object has a bar
method

(example from the net ..)
int counter = 0;
NSArray *subViews = [self.view subviews];
for (NSObject *item in subViews)
{
if ([item isKindOfClass:[UIImageView class]])
{
myObjects[counter++] = (UIImageView *) item;
}
else if ([item isMemberOfClass:[LogicBomb class]])
{
if ([item respondsToSelector: at (no spam) selector(detonate)])
[item detonate: [time seconds:10]];
}//else
}//if
}//for

like that ....

OH I saw this -- change methods around on the fly ..

In Objective-C 2.0 (Mac OS X 10.5 Leopard and later) the approved way
to do this uses the method_exchangeImplementations function instead.
As in:

Method frogs class_getInstanceMethod([MiniObject class], at (no spam) selector(generateFrogs));
Method lizards = class_getInstanceMethod([Controllerclass], at (no spam) selector
(generateLizards));

method_exchangeImplementations(lizards, frogs);
 
 
Page 1 of 1    
All times are GMT
The time now is Fri Nov 27, 2009 9:00 am