Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Objective-c > Runtime selecto...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 3 Topic 959 of 1009
Post > Topic >>

Runtime selectors and method calling

by "Jeff M." <massung@[EMAIL PROTECTED] > Feb 28, 2008 at 03:05 PM

I'm not at my machine at the moment, so please excuse any errors in my
example code below. I'm trying to get some runtime object/selector
calling to work, and running into a very weird problem. Do***entation
online (even on Apple's website) is pretty sparse on the subject, and
I'm hoping someone here can give me a good example or tell me where
I'm screwing up. :-)

I have two strings at runtime: the class and method name. Given the
do***entation I've read so far, my understanding is that I should be
able to do this:

void send_class_msg(const char* cls_name, const char* mthd_name)
{
    Class c = objc_getClass(name);
    SEL s = sel_registerName(mthd_name);

    [c performSelector: s];

    // or... objc_msgSend(c, s, ...);
}

That's a pretty trivial example, but generally gets the idea across.
The class value returned is just fine. If I replace #performSelector:
with #alloc, I get a new instance of the class and the universe is
right.

However, for some reason the selector is somehow _not_ right. I've
changed a lot of the code significantly, trying lots of other ideas to
compare the results. For example:

SEL s_good = @[EMAIL PROTECTED]
(alloc);
SEL s_bad = sel_registerName("alloc");

assert(s_good == s_bad); // this asserts true!!

[c performSelector: s_good]; // this works!
[c performSelector: s_bad]; // this does not work!

This is driving me nuts as I can't figure out what the hell is wrong.
My only guess is that @[EMAIL PROTECTED]
 is doing a little more at compile-time
than sel_registerName() is doing at runtime. But that's just a wild
guess.

In anticipation of what's coming next, I'm also going to need to send
runtime argument lists that aren't known at compile time. So,
objc_msgSend() won't fit my needs (the ... argument list won't cut
it). On the web it looks like there's a objc_msgSendv() function that
will probably work, but I can't tell if this is sup****ted by Apple or
is only special function for some Objective-C compilers? And I can't
find any do***entation for it. Does anyone have a link to a good
example of this in use?

Thanks in advance!

Jeff M.
 




 3 Posts in Topic:
Runtime selectors and method calling
"Jeff M." <m  2008-02-28 15:05:06 
Re: Runtime selectors and method calling
"Jeff M." <m  2008-02-28 19:12:36 
Re: Runtime selectors and method calling
Greg Parker <gparker@[  2008-02-28 21:23:29 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 23:49:09 CDT 2008.