• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Difference between SEL and const char* when sending a message?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Difference between SEL and const char* when sending a message?


  • Subject: Re: Difference between SEL and const char* when sending a message?
  • From: Michael Ash <email@hidden>
  • Date: Wed, 8 Apr 2009 10:27:44 -0400

On Tue, Apr 7, 2009 at 11:34 PM, Daqi Pei <email@hidden> 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.

It is helpful for things like this to actually say what happens. "The
approach above throws exceptions" is not very helpful. WHAT exceptions
does it throw? And since it's not the same code, WHAT exact code
throws these exceptions?

In any case, it's likely due to a feature of SELs that is not
duplicated when you use a constant string. While it is true that a SEL
is just a char * on the current 32-bit runtime, SELs have the
additional feature of being *unique* for any particular string. In
other words, two SELs with the same name always compare equal when
using ==, plain old pointer equality.

The runtime relies on this property for all kinds of things. By
passing in a naked C string, you're bypassing the uniquing mechanism,
and could easily end up with a string which has the same *contents*
but is not at the same address. This could break all sorts of things
and lead to the behavior that you see here. This is part of why you
must only create a SEL using the @selector directive or the
runtime/library functions made for this purpose.

Mike
_______________________________________________

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

  • Follow-Ups:
    • Re: Difference between SEL and const char* when sending a message?
      • From: Marcel Weiher <email@hidden>
References: 
 >Difference between SEL and const char* when sending a message? (From: Daqi Pei <email@hidden>)

  • Prev by Date: Re: Fully transparent window opaque to mouse?
  • Next by Date: Re: Storing bundle loaded main class instances in NSArray
  • Previous by thread: Re: Difference between SEL and const char* when sending a message?
  • Next by thread: Re: Difference between SEL and const char* when sending a message?
  • Index(es):
    • Date
    • Thread