Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Methods and pointers in Objective C

Methods and pointers in Objective C
Thread Tools
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status: Offline
Reply With Quote
Dec 27, 2000, 07:17 AM
 
What I want to do is to make a pointer to a method, kind of like you can make a pointer to a function in C. I know the class that I want to use, the only problem is that I won't know which method I want until runtime. I remember reading somewhere something that said if my method name was method, I could make a pointer and set it to that method by doing:

char *pointerToAMethod;
pointerToAMethod = NSStringFromSelector (method);

But not only does that not work, it also doesn't make any sense why it looks like I''d be returning a string object to a pointer to a character array. Also, I'm not sure if NSStringFromSelector is the correct function to call since I get a message telling me that method is undefined. (Well of course it is, it's not a variable, it's a name!) So I really don't know what I am doing here. I have tried to do the logical thing, make pointerToAMethod of type NSString* and put method in quotes. Putting method in quotes got rid of an error saying that method wasn't defined, and it didn'r create any new errors. The only problem is that when I try to use the pointer I get a warning saying that my class doesn't respond to that method... of course how can it say that considering that it doesn't know what method I've chosen until it is running? Anyway, if anyone could shed some light on this I'd be glad.
     
ali
Forum Regular
Join Date: Sep 2000
Status: Offline
Reply With Quote
Dec 27, 2000, 04:15 PM
 
You can use the SEL type to manipulate selectors in variables:

NSString *myString = @"setFoo:";
SEL mySelector;
mySelector= NSSelectorFromString(myString);

// later:
[myObject performSelector:mySelector withObject:anObjectArgument];

For instance, in target/action situations, the action is a SEL.

You can use @selector(setFoo to directly create a SEL type:

SEL mySelector = @selector(setFoo


Then there is also IMP, which is just like a function pointer. It is strongly bound to the class, so you have to obtain it from the object, given the selector:

IMP myMethodAddress = [myObject methodForSelector:mySelector];

It's usually best to use this with the right prototype, for instance:

typedef BOOL (*MyMethodImplementation)(NSString *n, int y, float h);

SEL sel = @selector(setName:birthYear:height;
MyMethodImplementation setNameMethod = (MyMethodImplementation)[myObject methodForSelector:sel];

//...later:
BOOL success = setNameMethod(myObject, sel, @"Joe", 1977, 181.5);

Note that the first two arguments to the actual function are the object itself and the selector; these are passed undercover under normal circumstances but must be done explicitly when doing the IMP thing.

In general using IMPs directly is pretty rare. You can get the same sort of indirection but with more flexibility using selectors.

One reason for using IMP is performance. There is a cost to obtaining the IMP (it's somewhat slower than sending the message directly); however, if you are going to loop and send the message thousands or more times, getting the IMP and invoking the function directly may help in performance. However, under normal circumstances, once the runtime caches things, message sending is usually very fast, so using IMP for performance might not give you a visible boost. Also, you have to be very careful in your use of IMPs as you have to make sure that the objects you are sending the same IMP to are of the same class.

Ali


[This message has been edited by ali (edited 12-27-2000).]
     
Dalgo  (op)
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status: Offline
Reply With Quote
Dec 28, 2000, 06:20 AM
 
Thanks I was able to use selectors correctly now. I will try using IMPs sometime later. Actually where I need to use the selector is withing a loop that is done anywhere from 10 to a few million times depending upon the input, so IMP's might be a good thing to look into.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 11:22 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2