Re: Difference between SEL and const char* when sending a message?
Re: Difference between SEL and const char* when sending a message?
- Subject: Re: Difference between SEL and const char* when sending a message?
- From: Graham Cox <email@hidden>
- Date: Wed, 8 Apr 2009 16:08:04 +1000
On 08/04/2009, at 1:34 PM, Daqi Pei wrote:
Hi everyone. I'm new to Objective-C but I've been working with C++ for
years. I'm trying to understand how the selector mechanism works.
So far it seems to me that SEL is simply a wrapper for 'const
char*', Obj-C
compiler maintains a table of all method name - method function
pointer
pairs and pick up one according to the object you passed to the
call. (Sorry
for my C++ semasiology)
My hypothesis works well with method calls with no parameters.
e.g. , you
cal write:
[myObj performSelector: (SEL)"someMethod"];
Though when it comes to method with parameters, it looks like a total
different story.
NSSelectorFromString(); works fine, but the approach above throws
exceptions.
Anyone knows why? Thanks.
SEL is actually declared as typedef struct objc_selector*, which is an
opaque struct with presumably private contents.
I believe it is implemented as some sort of table of constant strings,
but so what? There are plenty of documented ways to use SEL types, so
apart from intellectual curiosity, why are you trying to do something
unsupported?
selectors with parameters are of the form selector:param2:param3: and
the correct way to obtain the correct SEL of any named selector is to
use the @selector() function, i.e. [myObj
performSelector:@selector(someMethod)];
Don't cast to a const char*, it isn't correct even if that did happen
to be exactly how selectors were implemented.
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden