What's the type of a selector?

  • What's the type of a selector? I gather it's a SEL -- but what's a
    SEL, as far as trying to print one using NSLog is concerned?
    This attempt to find out compiles, but craps out when I try to run
    it, in an asm called objc_msgSend_rtp:

    - (void)foo {
        NSLog(@"%@", @selector(foo));
    } //end foo

    The Run log sez:
    [Session started at 2006-10-27 16:49:12 -0600.]
    2006-10-27 16:49:12.744 canonicalTest[1344] CFLog (0): CFMessagePort:
    bootstrap_register(): failed 1103 (0x44f), port = 0x3203, name =
    'com.yourcompany.canonicalTest.ServiceProvider'
    See /usr/include/servers/bootstrap_defs.h for the error codes.
    2006-10-27 16:49:12.745 canonicalTest[1344] CFLog (99):
    CFMessagePortCreateLocal(): failed to name Mach port
    (com.yourcompany.canonicalTest.ServiceProvider)
    canonicalTest has exited due to signal 11 (SIGSEGV).

    --Roland Silver <rollos...>
  • objc.h:
    typedef struct objc_selector     *SEL;

    On Oct 27, 2006, at 6:36 PM, Roland Silver wrote:

    > What's the type of a selector? I gather it's a SEL -- but what's a
    > SEL, as far as trying to print one using NSLog is concerned?
    > This attempt to find out compiles, but craps out when I try to run
    > it, in an asm called objc_msgSend_rtp:
    >
    > - (void)foo {
    > NSLog(@"%@", @selector(foo));
    > } //end foo
    >
    > The Run log sez:
    > [Session started at 2006-10-27 16:49:12 -0600.]
    > 2006-10-27 16:49:12.744 canonicalTest[1344] CFLog (0):
    > CFMessagePort: bootstrap_register(): failed 1103 (0x44f), port =
    > 0x3203, name = 'com.yourcompany.canonicalTest.ServiceProvider'
    > See /usr/include/servers/bootstrap_defs.h for the error codes.
    > 2006-10-27 16:49:12.745 canonicalTest[1344] CFLog (99):
    > CFMessagePortCreateLocal(): failed to name Mach port
    > (com.yourcompany.canonicalTest.ServiceProvider)
    > canonicalTest has exited due to signal 11 (SIGSEGV).
    >
    > --Roland Silver <rollos...>
    >
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/jstiles%
    > 40blizzard.com
    >
    > This email sent to <jstiles...>
  • NSStringFromSelector( @selector(foo) )

    On Oct 27, 2006, at 9:36 PM, Roland Silver wrote:

    > What's the type of a selector? I gather it's a SEL -- but what's a
    > SEL, as far as trying to print one using NSLog is concerned?
    > This attempt to find out compiles, but craps out when I try to run
    > it, in an asm called objc_msgSend_rtp:
    >
    > - (void)foo {
    > NSLog(@"%@", @selector(foo));
    > } //end foo
    >
    > The Run log sez:
    > [Session started at 2006-10-27 16:49:12 -0600.]
    > 2006-10-27 16:49:12.744 canonicalTest[1344] CFLog (0):
    > CFMessagePort: bootstrap_register(): failed 1103 (0x44f), port =
    > 0x3203, name = 'com.yourcompany.canonicalTest.ServiceProvider'
    > See /usr/include/servers/bootstrap_defs.h for the error codes.
    > 2006-10-27 16:49:12.745 canonicalTest[1344] CFLog (99):
    > CFMessagePortCreateLocal(): failed to name Mach port
    > (com.yourcompany.canonicalTest.ServiceProvider)
    > canonicalTest has exited due to signal 11 (SIGSEGV).
    >
    > --Roland Silver <rollos...>
    >
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<andrei...>
    >
    > This email sent to <andrei...>
  • It's a pointer to a struct objc_selector.

    Jump to definition in XCode is helpful for these things.

    You want %p to print the pointer value or consider using sel_getName ().

    - Chris

    On 28/10/2006, at 11:36 AM, Roland Silver wrote:

    > What's the type of a selector? I gather it's a SEL -- but what's a
    > SEL, as far as trying to print one using NSLog is concerned?
    > This attempt to find out compiles, but craps out when I try to run
    > it, in an asm called objc_msgSend_rtp:
    >
    > - (void)foo {
    > NSLog(@"%@", @selector(foo));
    > } //end foo
    >
    > The Run log sez:
    > [Session started at 2006-10-27 16:49:12 -0600.]
    > 2006-10-27 16:49:12.744 canonicalTest[1344] CFLog (0):
    > CFMessagePort: bootstrap_register(): failed 1103 (0x44f), port =
    > 0x3203, name = 'com.yourcompany.canonicalTest.ServiceProvider'
    > See /usr/include/servers/bootstrap_defs.h for the error codes.
    > 2006-10-27 16:49:12.745 canonicalTest[1344] CFLog (99):
    > CFMessagePortCreateLocal(): failed to name Mach port
    > (com.yourcompany.canonicalTest.ServiceProvider)
    > canonicalTest has exited due to signal 11 (SIGSEGV).
    >
    > --Roland Silver <rollos...>
    >
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<chris...>-
    > systems.com
    >
    > This email sent to <chris...>
    >

    --
    Coriolis Systems Limited is a limited company incorporated in England
    and Wales
    Company No. 5061807  Registered Office: 10 Oxford Street,
    Southampton, SO14 3DJ
  • If you want to log the method you are in use this:

    NSLog( @"%s", _cmd );

    Stephan

    On Oct 27, 2006, at 9:36 PM, Roland Silver wrote:

    > What's the type of a selector? I gather it's a SEL -- but what's a
    > SEL, as far as trying to print one using NSLog is concerned?
    > This attempt to find out compiles, but craps out when I try to run
    > it, in an asm called objc_msgSend_rtp:
    >
    > - (void)foo {
    > NSLog(@"%@", @selector(foo));
    > } //end foo
    >
    > The Run log sez:
    > [Session started at 2006-10-27 16:49:12 -0600.]
    > 2006-10-27 16:49:12.744 canonicalTest[1344] CFLog (0):
    > CFMessagePort: bootstrap_register(): failed 1103 (0x44f), port =
    > 0x3203, name = 'com.yourcompany.canonicalTest.ServiceProvider'
    > See /usr/include/servers/bootstrap_defs.h for the error codes.
    > 2006-10-27 16:49:12.745 canonicalTest[1344] CFLog (99):
    > CFMessagePortCreateLocal(): failed to name Mach port
    > (com.yourcompany.canonicalTest.ServiceProvider)
    > canonicalTest has exited due to signal 11 (SIGSEGV).
    >
    > --Roland Silver <rollos...>
    >
  • No, Stephan, I DON'T want to log the method! I want to find out what
    the type of a SELECTOR is, eg:
    ITEM          TYPE
    "foo"          char*
    69            int
    105.0          float or double
    @"bar"        NSString*
    @selector(foo) ???
    SEL            ???

    AND...
    I know how to NSLog a char* (%s) or an int (%i) or a float or double
    (%g, etc) or an NSString* (%@), BUT...

    ..HOW DO I NSLOG A SELECTOR???

    --Roland Silver <rollos...>
    -------------------------------------

    On Oct 27, 2006, at 7:46 PM, Stephan Cleaves wrote:

    > If you want to log the method you are in use this:
    >
    > NSLog( @"%s", _cmd );
    >
    > Stephan
  • On Oct 27, 2006, at 9:36 PM, Roland Silver wrote:

    > What's the type of a selector? I gather it's a SEL -- but what's a
    > SEL, as far as trying to print one using NSLog is concerned?
    > This attempt to find out compiles, but craps out when I try to run
    > it, in an asm called objc_msgSend_rtp:
    >
    > - (void)foo {
    > NSLog(@"%@", @selector(foo));
    > } //end foo

    A selector is a SEL, which is typed as

    typedef struct objc_selector     *SEL;

    To log one you write

    NSLog(@"%@", NSStringFromSelector(aSelector));

    Jim
  • On 10/27/06, Roland Silver <rollos...> wrote:
    > No, Stephan, I DON'T want to log the method! I want to find out what
    > the type of a SELECTOR is, eg:
    > ITEM          TYPE
    > "foo"          char*
    > 69            int
    > 105.0          float or double
    > @"bar"        NSString*
    > @selector(foo) ???
    > SEL            ???
    >
    > AND...
    > I know how to NSLog a char* (%s) or an int (%i) or a float or double
    > (%g, etc) or an NSString* (%@), BUT...
    >
    > ..HOW DO I NSLOG A SELECTOR???

    NSLog(@"%@", NSStringFromSelector(aSelector));

    --
    Clark S. Cox III
    <clarkcox3...>
  • On Oct 27, 2006, at 7:45 PM, John Stiles wrote:

    > objc.h:
    > typedef struct objc_selector     *SEL;

    John, the question remains, what do I put in for
          %??
    in
          obj_selector bar =  @selector(foo);
          NSLog(@"%??", bar);
    in other words, what's an obj_selector?

    Aaron Hillegass gave me a straight answer to my original question:
    -------------------------
    It is actually a char *:
      - (void)foo {
        NSLog(@"%s", @selector(foo));
      }
    On Oct 27, 2006, at 6:52 PM, Roland Silver wrote:
    > What's the type of a selector? I gather it's a SEL -- but what's a
    > SEL, as far as trying to print one using NSLog is concerned?
    -------------------------
    So I gather that a SEL is merely a char*. Que no?
  • Hi,

    Looks like you should read some C books.

    It's a pointer to an structure.

    It's not a char * and NSLog ("%s", @selector (foo)) will not work as
    someone suggested earlier.

    Either use %p for a pointer or use %s with sel_getName or use %@ and
    NSStringFromSelector.

    This is all in the documentation and you really don't need us to help
    you.

    - Chris

    On 28/10/2006, at 12:24 PM, Roland Silver wrote:

    > On Oct 27, 2006, at 7:45 PM, John Stiles wrote:
    >
    >> objc.h:
    >> typedef struct objc_selector     *SEL;
    >
    > John, the question remains, what do I put in for
    > %??
    > in
    > obj_selector bar =  @selector(foo);
    > NSLog(@"%??", bar);
    > in other words, what's an obj_selector?
    >
    > Aaron Hillegass gave me a straight answer to my original question:
    > -------------------------
    > It is actually a char *:
    > - (void)foo {
    > NSLog(@"%s", @selector(foo));
    > }
    > On Oct 27, 2006, at 6:52 PM, Roland Silver wrote:
    >> What's the type of a selector? I gather it's a SEL -- but what's a
    >> SEL, as far as trying to print one using NSLog is concerned?
    > -------------------------
    > So I gather that a SEL is merely a char*. Que no?
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<chris...>-
    > systems.com
    >
    > This email sent to <chris...>
    >
  • On Oct 27, 2006, at 8:30 PM, Chris Suter wrote:
    > Hi,
    > Looks like you should read some C books.
    > It's a pointer to an structure.
    > It's not a char * and NSLog ("%s", @selector (foo)) will not work
    > as someone suggested earlier.
    > Either use %p for a pointer or use %s with sel_getName or use %@
    > and NSStringFromSelector.
    > This is all in the documentation and you really don't need us to
    > help you.
    > - Chris

    Well, Chris...
    First: I've read Kernighan/Ritchie and Harbison/Steele; will they do?

    Second: I'm sure there isn't a book on C that talks about @selector
    or NSLog, which are Cocoa features, not C features.

    Third, NSLog ("%s", @selector (foo)) DOES work, as Aaron Hillegass
    suggested: try it for yourself; I did! Here ya go:

    @implementation type
    - (void)awakeFromNib {
    NSLog(@"%s", @selector(awakeFromNib));
    }
    @end

    That is the important part of a project that compiles and runs. The
    result from the run log is:

    [Session started at 2006-10-27 20:46:42 -0600.]
    2006-10-27 20:46:43.246 selectorType[1489] awakeFromNib

    Fourth: It doesn't look to me like @selector is a pointer to a struct
    (if that's what you meant), it looks like a pointer-to-char, i.e. a C-
    string.

    Fifth: Thanks for telling me that it's "all in the documentation" and
    I don't need the list members to help me. I searched around at length
    in the "documentation" before putting my question to the list, but
    didn't find the answer anywhere I looked.

    --Roland Silver <rollos...>
    --------------------------------------
  • > Second: I'm sure there isn't a book on C that talks about @selector
    > or NSLog, which are Cocoa features, not C features.

    Actually, these are Objective C features.  Cocoa is a set of classes
    built on top of Objective C.

    > Fourth: It doesn't look to me like @selector is a pointer to a
    > struct (if that's what you meant), it looks like a pointer-to-char,
    > i.e. a C-string.

    I don't see it defined that way.  objc.h:

    typedef struct objc_selector  *SEL;

    If it looks like a char*, that's an implementation detail that you
    should not rely on.

    > Fifth: Thanks for telling me that it's "all in the documentation"
    > and I don't need the list members to help me. I searched around at
    > length in the "documentation" before putting my question to the
    > list, but didn't find the answer anywhere I looked.

    <http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/
    index.html
    >

    Specifically:

    <http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/
    Articles/chapter_4_section_6.html
    >

    Specifically:

    "Selectors

    "For efficiency, full ASCII names are not used as method selectors in
    compiled code. Instead, the compiler writes each method name into a
    table, then pairs the name with a unique identifier that represents
    the method at runtime."

    So the fact that right now you can get away with casting them to a
    char* does not mean that it won't change in the future.

    Finally, something about flies and vinegar versus honey.  Please
    don't be rude to the people you're asking for help.

    Stephen Deken
    <stephen.deken...>
  • Am 28.10.2006 um 05:05 schrieb Roland Silver:

    > On Oct 27, 2006, at 8:30 PM, Chris Suter wrote:
    >> Hi,
    >> Looks like you should read some C books.
    >> It's a pointer to an structure.
    >> It's not a char * and NSLog ("%s", @selector (foo)) will not work
    >> as someone suggested earlier.
    >> Either use %p for a pointer or use %s with sel_getName or use %@
    >> and NSStringFromSelector.
    >> This is all in the documentation and you really don't need us to
    >> help you.
    >> - Chris
    >
    > Well, Chris...

    Aaron Hillegass writes in the 2nd Edition of his Cocoa Programming
    for Mac OS X in Chapter 3 "Objective-C / For the More Curious: How
    Does Messageing Work?", p78f:
    "The selector is of type SEL. Although, SEL is actually defined to be
    char * ...".

    With Cmd-doubleclick on any SEL in Xcode I find immediately:
    typedef struct objc-selector *SEL
    in the header objc.h

    There is a little bit more info in the Objective-C runtime reference.

    Cheers.
    Klaus
  • Hi,

    I just wrote an apology to Roland off-list.

    I didn't realise that you could cast SEL's to char * but from reading
    the docs. it appears you can. It was probably originally intended
    that objc_selector be an opaque type but given it's now documented
    that you can cast to char *, it's very unlikely it will change.

    Looks like I should go read some books. :-)

    - Chris

    On 28/10/2006, at 2:11 PM, Klaus Backert wrote:

    >
    > Am 28.10.2006 um 05:05 schrieb Roland Silver:
    >
    >> On Oct 27, 2006, at 8:30 PM, Chris Suter wrote:
    >>> Hi,
    >>> Looks like you should read some C books.
    >>> It's a pointer to an structure.
    >>> It's not a char * and NSLog ("%s", @selector (foo)) will not work
    >>> as someone suggested earlier.
    >>> Either use %p for a pointer or use %s with sel_getName or use %@
    >>> and NSStringFromSelector.
    >>> This is all in the documentation and you really don't need us to
    >>> help you.
    >>> - Chris
    >>
    >> Well, Chris...
    >
    > Aaron Hillegass writes in the 2nd Edition of his Cocoa Programming
    > for Mac OS X in Chapter 3 "Objective-C / For the More Curious: How
    > Does Messageing Work?", p78f:
    > "The selector is of type SEL. Although, SEL is actually defined to
    > be char * ...".
    >
    > With Cmd-doubleclick on any SEL in Xcode I find immediately:
    > typedef struct objc-selector *SEL
    > in the header objc.h
    >
    > There is a little bit more info in the Objective-C runtime reference.
    >
    > Cheers.
    > Klaus
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<chris...>-
    > systems.com
    >
    > This email sent to <chris...>
    >

    --
    Coriolis Systems Limited is a limited company incorporated in England
    and Wales
    Company No. 5061807  Registered Office: 10 Oxford Street,
    Southampton, SO14 3DJ
  • On Oct 27, 2006, at 20:05, Roland Silver wrote:

    > Fourth: It doesn't look to me like @selector is a pointer to a
    > struct (if that's what you meant), it looks like a pointer-to-char,
    > i.e. a C-string.
    >
    > Fifth: Thanks for telling me that it's "all in the documentation"
    > and I don't need the list members to help me. I searched around at
    > length in the "documentation" before putting my question to the
    > list, but didn't find the answer anywhere I looked.

    <http://developer.apple.com/documentation/Cocoa/Reference/ObjCRuntimeRef/Ref
    erence/reference.html#//apple_ref/c/func/sel_getName
    > tells you that it can also be cast to a C string (const char *).

    Adam
  • Someone just pointed me at:

    http://lists.apple.com/archives/Objc-language/2006/Aug/msg00020.html

    Don't believe anything I say today!

    I've filed a documentation bug.

    - Chris

    On 28/10/2006, at 2:15 PM, Chris Suter wrote:

    > Hi,
    >
    > I just wrote an apology to Roland off-list.
    >
    > I didn't realise that you could cast SEL's to char * but from
    > reading the docs. it appears you can. It was probably originally
    > intended that objc_selector be an opaque type but given it's now
    > documented that you can cast to char *, it's very unlikely it will
    > change.
    >
    > Looks like I should go read some books. :-)
    >
    > - Chris
    >
    > On 28/10/2006, at 2:11 PM, Klaus Backert wrote:
    >
    >>
    >> Am 28.10.2006 um 05:05 schrieb Roland Silver:
    >>
    >>> On Oct 27, 2006, at 8:30 PM, Chris Suter wrote:
    >>>> Hi,
    >>>> Looks like you should read some C books.
    >>>> It's a pointer to an structure.
    >>>> It's not a char * and NSLog ("%s", @selector (foo)) will not
    >>>> work as someone suggested earlier.
    >>>> Either use %p for a pointer or use %s with sel_getName or use %@
    >>>> and NSStringFromSelector.
    >>>> This is all in the documentation and you really don't need us to
    >>>> help you.
    >>>> - Chris
    >>>
    >>> Well, Chris...
    >>
    >> Aaron Hillegass writes in the 2nd Edition of his Cocoa Programming
    >> for Mac OS X in Chapter 3 "Objective-C / For the More Curious: How
    >> Does Messageing Work?", p78f:
    >> "The selector is of type SEL. Although, SEL is actually defined to
    >> be char * ...".
    >>
    >> With Cmd-doubleclick on any SEL in Xcode I find immediately:
    >> typedef struct objc-selector *SEL
    >> in the header objc.h
    >>
    >> There is a little bit more info in the Objective-C runtime reference.
    >>
    >> Cheers.
    >> Klaus
    >> _______________________________________________
    >> Do not post admin requests to the list. They will be ignored.
    >> Cocoa-dev mailing list      (<Cocoa-dev...>)
    >> Help/Unsubscribe/Update your Subscription:
    >> http://lists.apple.com/mailman/options/cocoa-dev/<chris...>-
    >> systems.com
    >>
    >> This email sent to <chris...>
    >>
    >
    > --
    > Coriolis Systems Limited is a limited company incorporated in
    > England and Wales
    > Company No. 5061807  Registered Office: 10 Oxford Street,
    > Southampton, SO14 3DJ
    >
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<chris...>-
    > systems.com
    >
    > This email sent to <chris...>
    >

    --
    Coriolis Systems Limited is a limited company incorporated in England
    and Wales
    Company No. 5061807  Registered Office: 10 Oxford Street,
    Southampton, SO14 3DJ
  • To make it even clearer:

    SEL is indeed a struct, but a struct containing nothing but a char*.
    If I define an example struct consisting of nothing but a char *,
    logging this with %s indeed gives the string put into the struct.

    typedef struct PMText {
        char* text;
    } PMText;

    If I then use this and log it with %s:

    char someText[] = "hello world";

    PMText foo;
    foo.text = someText;

    printf("%s", foo);

    This reproduces "hello world", as expected with no magic involved,
    just plain C.

    The important point is that if the documentation says a SEL is
    defined as a struct, it may be wise to treat it as such, as maybe at
    some point the exact definition of the struct may be changed (though
    this is unlikely for something as basic as SEL). Let's say that I
    would want to expand my PMText with some other variable:

    typedef struct PMText {
        float value;
        char* text;
    } PMText;

    If I now use the same code as above to log the struct, my app
    crashes. However, accessing the structs string as it should, so
    using .text is no problem, and the new implementation of PMText does
    not break the old one if properly used.

    Patrick

    On 27-Oct-06, at 23:05 PM, Roland Silver wrote:

    > On Oct 27, 2006, at 8:30 PM, Chris Suter wrote:
    >> Hi,
    >> Looks like you should read some C books.
    >> It's a pointer to an structure.
    >> It's not a char * and NSLog ("%s", @selector (foo)) will not work
    >> as someone suggested earlier.
    >> Either use %p for a pointer or use %s with sel_getName or use %@
    >> and NSStringFromSelector.
    >> This is all in the documentation and you really don't need us to
    >> help you.
    >> - Chris
    >
    > Well, Chris...
    > First: I've read Kernighan/Ritchie and Harbison/Steele; will they do?
    >
    > Second: I'm sure there isn't a book on C that talks about @selector
    > or NSLog, which are Cocoa features, not C features.
    >
    > Third, NSLog ("%s", @selector (foo)) DOES work, as Aaron Hillegass
    > suggested: try it for yourself; I did! Here ya go:
    >
    > @implementation type
    > - (void)awakeFromNib {
    > NSLog(@"%s", @selector(awakeFromNib));
    > }
    > @end
    >
    > That is the important part of a project that compiles and runs. The
    > result from the run log is:
    >
    > [Session started at 2006-10-27 20:46:42 -0600.]
    > 2006-10-27 20:46:43.246 selectorType[1489] awakeFromNib
    >
    > Fourth: It doesn't look to me like @selector is a pointer to a
    > struct (if that's what you meant), it looks like a pointer-to-char,
    > i.e. a C-string.
    >
    > Fifth: Thanks for telling me that it's "all in the documentation"
    > and I don't need the list members to help me. I searched around at
    > length in the "documentation" before putting my question to the
    > list, but didn't find the answer anywhere I looked.
    >
    > --Roland Silver <rollos...>
    > --------------------------------------
    >
    >
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/meirmans%
    > 40sympatico.ca
    >
    > This email sent to <meirmans...>
  • On Oct 27, 2006, at 7:24 PM, Roland Silver wrote:

    > John, the question remains, what do I put in for
    > %??
    > in
    > obj_selector bar =  @selector(foo);
    > NSLog(@"%??", bar);
    > in other words, what's an obj_selector?
    >
    > Aaron Hillegass gave me a straight answer to my original question:
    > -------------------------
    > It is actually a char *:
    > - (void)foo {
    > NSLog(@"%s", @selector(foo));
    > }
    > On Oct 27, 2006, at 6:52 PM, Roland Silver wrote:
    >> What's the type of a selector? I gather it's a SEL -- but what's a
    >> SEL, as far as trying to print one using NSLog is concerned?
    > -------------------------
    > So I gather that a SEL is merely a char*. Que no?

    If you want to log a selector -- a SEL -- then use:

    NSLog(@"The selector is: %@",
    NSStringFromSelector(someRandomSelector));

    Which can be useful in any random method's implementation in that you
    can do this:

    NSLog(@"The currently executing method is: %@",
    NSStringFromSelector(_cmd));

    The above is the "most correct", "least fragile", pattern for logging
    the selector.

    Now, SEL is defined as:

    typedef struct objc_selector    *SEL;

    Which really doesn't help much unless you know the definition of an
    objc_selector.  Now, unfortunately, the above is anonymous or forward
    structure declaration.  It really only says that SEL is a pointer to a
    structure whose content this particular header doesn't think you need
    to know about.  Fortunately, objc source is available and you can
    download the source from the darwin repository (http://www.opensource.apple.com/darwinsource/10.4.7.ppc/
    ).

    That objc treats SEL as (char *) internally is an implementation
    detail.  It may even be a relatively documented implementation detail,
    but a detail it is and it is a detail that'll lead to less efficient
    coding, if you aren't careful.

    For example, Cocoa's standard menu validation mechanism enables/
    disables menu items based on their action.  An action is just an
    SEL.  Equating SEL to (char *) would indicate that one should use
    strcmp() to determine if a particular action is equivalent to a
    particular selector (i.e. if the menu item invokes the -copy: action
    and you want to disable the item if a particular object is selected),
    but Cocoa is documented as requiring the following validation pattern:

    - (BOOL)validateMenuItem:(NSMenuItem *)item {
        if ([item action] == @selector(copy:))
            return NO;

        return YES;
    }

    So, YES, an SEL is a (char*).  But, NO, that doesn't mean that SEL
    doesn't come with additional rules, requirements, and opportunities.

    You certainly could do...

    NSLog(@"The currently executing method is: %s", (char
    *)@selector(copy:));

    .... but that isn't as clean as using the API.  The typecast assumes
    knowledge of the API that is not reflected in the header file.  It
    effectively prevents the compiler from doing its validation job.
    Furthermore, sticking with the non-cast-required higher level APIs
    guarantees that your code won't break even if the typedef of SEL were
    to change -- unlikely, but it could happen if Apple were to decide
    that there was a huge benefit to changing the ObjC ABI.

    b.bum
  • Yes, thanks, Bill. I'm respectful of the idea that the type of
    objc_selector -- hence of SEL* -- is hidden, and might change with
    time. What I didn't know (but has been pointed out since) is the
    existence of the NSStringFromSelector function, which is sufficient
    to answer the other part of my question: how to print the selector.

    --Roland Silver <rollos...>

    On Oct 28, 2006, at 12:26 AM, Bill Bumgarner wrote:

    > On Oct 27, 2006, at 7:24 PM, Roland Silver wrote:
    >
    >> John, the question remains, what do I put in for
    >> %??
    >> in
    >> obj_selector bar =  @selector(foo);
    >> NSLog(@"%??", bar);
    >> in other words, what's an obj_selector?
    >>
    >> Aaron Hillegass gave me a straight answer to my original question:
    >> -------------------------
    >> It is actually a char *:
    >> - (void)foo {
    >> NSLog(@"%s", @selector(foo));
    >> }
    >> On Oct 27, 2006, at 6:52 PM, Roland Silver wrote:
    >>> What's the type of a selector? I gather it's a SEL -- but what's
    >>> a SEL, as far as trying to print one using NSLog is concerned?
    >> -------------------------
    >> So I gather that a SEL is merely a char*. Que no?
    >
    > If you want to log a selector -- a SEL -- then use:
    >
    > NSLog(@"The selector is: %@", NSStringFromSelector
    > (someRandomSelector));
    >
    > Which can be useful in any random method's implementation in that
    > you can do this:
    >
    > NSLog(@"The currently executing method is: %@",
    > NSStringFromSelector(_cmd));
    >
    > The above is the "most correct", "least fragile", pattern for
    > logging the selector.
    >
    > Now, SEL is defined as:
    >
    > typedef struct objc_selector    *SEL;
    >
    > Which really doesn't help much unless you know the definition of an
    > objc_selector.  Now, unfortunately, the above is anonymous or
    > forward structure declaration.  It really only says that SEL is a
    > pointer to a structure whose content this particular header doesn't
    > think you need to know about.  Fortunately, objc source is
    > available and you can download the source from the darwin
    > repository (http://www.opensource.apple.com/darwinsource/10.4.7.ppc/).
    >
    > That objc treats SEL as (char *) internally is an implementation
    > detail.  It may even be a relatively documented implementation
    > detail, but a detail it is and it is a detail that'll lead to less
    > efficient coding, if you aren't careful.
    >
    > For example, Cocoa's standard menu validation mechanism enables/
    > disables menu items based on their action.  An action is just an
    > SEL.  Equating SEL to (char *) would indicate that one should use
    > strcmp() to determine if a particular action is equivalent to a
    > particular selector (i.e. if the menu item invokes the -copy:
    > action and you want to disable the item if a particular object is
    > selected), but Cocoa is documented as requiring the following
    > validation pattern:
    >
    > - (BOOL)validateMenuItem:(NSMenuItem *)item {
    > if ([item action] == @selector(copy:))
    > return NO;
    >
    > return YES;
    > }
    >
    > So, YES, an SEL is a (char*).  But, NO, that doesn't mean that SEL
    > doesn't come with additional rules, requirements, and opportunities.
    >
    > You certainly could do...
    >
    > NSLog(@"The currently executing method is: %s", (char *)@selector
    > (copy:));
    >
    > .... but that isn't as clean as using the API.  The typecast
    > assumes knowledge of the API that is not reflected in the header
    > file.  It effectively prevents the compiler from doing its
    > validation job.  Furthermore, sticking with the non-cast-required
    > higher level APIs guarantees that your code won't break even if the
    > typedef of SEL were to change -- unlikely, but it could happen if
    > Apple were to decide that there was a huge benefit to changing the
    > ObjC ABI.
    >
    > b.bum
    >

    --Roland Silver <rollos...>
  • Am 28.10.2006 um 04:30 schrieb Chris Suter:
    > Looks like you should read some C books.

      All of us should do that, actually. It's never a bad idea to learn
    more.

    > It's a pointer to an structure.
    >
    > It's not a char * and NSLog ("%s", @selector (foo)) will not work
    > as someone suggested earlier.

      Have you actually tried that?

      Because if you had, you would have found that Spephan was
    completely correct. A SEL is, or at least starts with, a "uniqued" C
    string pointer. So, you can actually typecast it to a string and it
    will just work. Of course, that's making a lot of assumptions, and I
    would recommend the NSStringFromSelector() or sel_getName() approach
    because they're both documented and safer.

      _cmd is an invisible first parameter passed to every ObjC method
    that contains the current method's SEL.

    Cheers,
    -- M. Uli Kusterer
    http://www.zathras.de
  • On Oct 28, 2006, at 7:26 AM, Bill Bumgarner wrote:

    > On Oct 27, 2006, at 7:24 PM, Roland Silver wrote:
    >
    >> So I gather that a SEL is merely a char*. Que no?

    Bill gave rather a long, if very accurate, explanation.  SEL values
    are made unique by the runtime/linker on OS X.  This is why you can
    compare the pointer values (which is what you're doing when comparing
    SEL values) directly, and the runtime relies on this too for
    performance reasons.

    The upshot of this is that you *cannot* use a string in place of a
    SEL; you must always write @selector(<name of selector>).

    Also, Bill's right that you should use the API to get the string
    value.  I don't know (because I haven't checked) whether casting to
    char * still works on the 64-bit ObjC ABI in Leopard, for instance.
    It probably does, but using the API means you're guaranteed your code
    will work.  Relying on implementation details doesn't; even
    documented ones sometimes change.

    Kind regards,

    Alastair.

    --
    http://alastairs-place.net
  • On Oct 29, 2006, at 5:34 AM, Alastair Houghton wrote:

    > Also, Bill's right that you should use the API to get the string
    > value.  I don't know (because I haven't checked) whether casting to
    > char * still works on the 64-bit ObjC ABI in Leopard, for
    > instance.  It probably does, but using the API means you're
    > guaranteed your code will work.  Relying on implementation details
    > doesn't; even documented ones sometimes change.

    Don't rely on your knowledge of the 32-bit ObjC ABI to guide you in
    the 64-bit ABI. Implementation details are changing, and writing code
    that relies on these details will only cause you issues when you
    reach the point in time that you decide you want to move your
    application to 64-bit.
    --
    David Duncan
    Apple DTS Quartz and Printing
    <david.duncan...>
  • On 30 Oct 2006, at 17:02, David Duncan wrote:

    > On Oct 29, 2006, at 5:34 AM, Alastair Houghton wrote:
    >
    >> Also, Bill's right that you should use the API to get the string
    >> value.  I don't know (because I haven't checked) whether casting
    >> to char * still works on the 64-bit ObjC ABI in Leopard, for
    >> instance.  It probably does, but using the API means you're
    >> guaranteed your code will work.  Relying on implementation details
    >> doesn't; even documented ones sometimes change.
    >
    > Don't rely on your knowledge of the 32-bit ObjC ABI to guide you in
    > the 64-bit ABI. Implementation details are changing, and writing
    > code that relies on these details will only cause you issues when
    > you reach the point in time that you decide you want to move your
    > application to 64-bit.

    I think that's what I just said :-)  Or at least it's very similar.
    Anyway, it's good advice.

    Kind regards,

    Alastair.

    --
    http://alastairs-place.net
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today