Getting NSApplicationDelegate protocol

  • Hello,

    I'm trying to get the methods a protocol specifies and just stumbled upon
    one problem: the following code returns NULL:

        Protocol *protocol = objc_getProtocol("NSApplicationDelegate");

    I saw (
    http://stackoverflow.com/questions/10212119/objc-getprotocol-returns-null-f
    or-nsapplicationdelegate
    )
    that the it is because the protocol was not:

        Adopted by a class,
        Or referred to somewhere in source code (using @protocol())

    As am doing this at runtime, I cannot use "@protocol()", right?

    So I thought I will add the protocol to the class:

        Protocol *protocol = objc_getProtocol("NSApplicationDelegate");
        BOOL class_addProtocol(class, protocol);

    The problem, obviously, is that I cannot add a protocol until it been
    created and the protocol wont be created until used be a class.

    Is there a way to get NSApplicationDelegate protocol besides the
    compile-time "@protocol()"?

    Also, when I do:

        Protocol *protocol = objc_getProtocol("NSTextViewDelegate");

    return the protocol even when I didn't create any textviews...?

    PS: To get all the method descriptions of a protocol, do I have to call
    four times "protocol_getMethodDescription"?

        protocol_getMethodDescription(p, aSel, NO, NO);
        protocol_getMethodDescription(p, aSel, NO, YES);
        protocol_getMethodDescription(p, aSel, YES, NO);
        protocol_getMethodDescription(p, aSel, YES, YES);
  • On Fri, Jul 6, 2012 at 3:30 AM, ecir hana <ecir.hana...> wrote:
    > I'm trying to get the methods a protocol specifies and just stumbled upon
    > one problem: the following code returns NULL:
    >
    > Protocol *protocol = objc_getProtocol("NSApplicationDelegate");

    Are you trying this on 10.5? Or are you building this application with
    the 10.5 SDK?

    Before 10.6 the NSApplicationDelegate was an informal protocol (that
    is, a category of methods on NSObject). This is because "@optional"
    didn't appear till the 10.6 SDK/compiler and previous to that for
    protocols:

    1) all methods were mandatory
    2) you couldn't extend the protocol later; once you publish it's set
    of methods in a Framework you were fixed at that set

    Point (2) follows from point (1) if you think about it enough.

    So some of the protocols, like NSApplicationDelegate, weren't really
    protocols at all. But in 10.6 they introduced "@optional", and
    NSApplicationDelegate became a formal protocol.
  • Sorry I should've said that before: no, I'm on 10.6.

    But thanks for the reply!

    On Fri, Jul 6, 2012 at 11:14 AM, Stephen J. Butler <stephen.butler...>
    > wrote:

    > On Fri, Jul 6, 2012 at 3:30 AM, ecir hana <ecir.hana...> wrote:
    >> I'm trying to get the methods a protocol specifies and just stumbled upon
    >> one problem: the following code returns NULL:
    >>
    >> Protocol *protocol = objc_getProtocol("NSApplicationDelegate");
    >
    > Are you trying this on 10.5? Or are you building this application with
    > the 10.5 SDK?
    >
    > Before 10.6 the NSApplicationDelegate was an informal protocol (that
    > is, a category of methods on NSObject). This is because "@optional"
    > didn't appear till the 10.6 SDK/compiler and previous to that for
    > protocols:
    >
    > 1) all methods were mandatory
    > 2) you couldn't extend the protocol later; once you publish it's set
    > of methods in a Framework you were fixed at that set
    >
    > Point (2) follows from point (1) if you think about it enough.
    >
    > So some of the protocols, like NSApplicationDelegate, weren't really
    > protocols at all. But in 10.6 they introduced "@optional", and
    > NSApplicationDelegate became a formal protocol.
    >
  • On Jul 6, 2012, at 3:30 AM, ecir hana wrote:

    > I'm trying to get the methods a protocol specifies and just stumbled upon
    > one problem: the following code returns NULL:
    >
    > Protocol *protocol = objc_getProtocol("NSApplicationDelegate");
    >
    > I saw (
    > http://stackoverflow.com/questions/10212119/objc-getprotocol-returns-null-f
    or-nsapplicationdelegate
    )
    > that the it is because the protocol was not:
    >
    > Adopted by a class,
    > Or referred to somewhere in source code (using @protocol())
    >
    > As am doing this at runtime, I cannot use "@protocol()", right?

    > Is there a way to get NSApplicationDelegate protocol besides the
    > compile-time "@protocol()"?

    No.  The run-time information about the protocol is obtained from information that would have to have been baked into the executable at build time.

    > Also, when I do:
    >
    > Protocol *protocol = objc_getProtocol("NSTextViewDelegate");
    >
    > return the protocol even when I didn't create any textviews...?

    You have presumably loaded AppKit, and AppKit does have the protocol details for NSTextViewDelegate baked into it, because it includes classes which adopt it and/or code which uses @protocol(NSTextViewDelegate).

    Regards,
    Ken
  • On Fri, Jul 6, 2012 at 11:56 AM, Ken Thomases <ken...> wrote:

    >> Is there a way to get NSApplicationDelegate protocol besides the
    >> compile-time "@protocol()"?
    >
    > No.  The run-time information about the protocol is obtained from
    > information that would have to have been baked into the executable at build
    > time.
    >
    >
    >> Also, when I do:
    >>
    >> Protocol *protocol = objc_getProtocol("NSTextViewDelegate");
    >>
    >> return the protocol even when I didn't create any textviews...?
    >
    > You have presumably loaded AppKit, and AppKit does have the protocol
    > details for NSTextViewDelegate baked into it, because it includes classes
    > which adopt it and/or code which uses @protocol(NSTextViewDelegate).
    >

    Thank you. And please, can you explain to me why is it that case? Is it
    simply because AppKit includes the definition of NSTextViewDelegate
    protocol and not includes one for NSApplicationDelegate? Is it somewhere
    documented which protocols are not included? I mean,
    besides NSApplicationDelegate, what other delegate definitions cannot be
    obtained at runtime? Or is there anything like AppKit which, after being
    loaded, provides the definitions?

    And for the ones includes, how to get all the methods described?
  • On Fri, Jul 6, 2012 at 4:35 AM, ecir hana <ecir.hana...> wrote:
    > Sorry I should've said that before: no, I'm on 10.6.
    >
    > But thanks for the reply!

    You're positive you're linking against the 10.6 SDK? Even if you're on
    10.6 you might be linking against the 10.5 SDK and that would explain
    your problem perfectly.

    > On Fri, Jul 6, 2012 at 11:14 AM, Stephen J. Butler <stephen.butler...>
    >> wrote:
    >
    >> On Fri, Jul 6, 2012 at 3:30 AM, ecir hana <ecir.hana...> wrote:
    >>> I'm trying to get the methods a protocol specifies and just stumbled upon
    >>> one problem: the following code returns NULL:
    >>>
    >>> Protocol *protocol = objc_getProtocol("NSApplicationDelegate");
    >>
    >> Are you trying this on 10.5? Or are you building this application with
    >> the 10.5 SDK?
    >>
    >> Before 10.6 the NSApplicationDelegate was an informal protocol (that
    >> is, a category of methods on NSObject). This is because "@optional"
    >> didn't appear till the 10.6 SDK/compiler and previous to that for
    >> protocols:
    >>
    >> 1) all methods were mandatory
    >> 2) you couldn't extend the protocol later; once you publish it's set
    >> of methods in a Framework you were fixed at that set
    >>
    >> Point (2) follows from point (1) if you think about it enough.
    >>
    >> So some of the protocols, like NSApplicationDelegate, weren't really
    >> protocols at all. But in 10.6 they introduced "@optional", and
    >> NSApplicationDelegate became a formal protocol.
    >>

  • On Fri, Jul 6, 2012 at 8:01 PM, Stephen J. Butler
    <stephen.butler...>wrote:

    >
    > You're positive you're linking against the 10.6 SDK? Even if you're on
    > 10.6 you might be linking against the 10.5 SDK and that would explain
    > your problem perfectly.
    >
    >
    NSAppKitVersionNumber says "1038.36" which is 10.6.
  • On Fri, Jul 6, 2012, at 10:00 PM, ecir hana wrote:
    > On Fri, Jul 6, 2012 at 8:01 PM, Stephen J. Butler
    > <stephen.butler...>wrote:
    >
    >>
    >> You're positive you're linking against the 10.6 SDK? Even if you're on
    >> 10.6 you might be linking against the 10.5 SDK and that would explain
    >> your problem perfectly.
    >>
    >>
    > NSAppKitVersionNumber says "1038.36" which is 10.6.

    That tells you what version of AppKit you're _running on_, not what
    version you've _linked against._ The whole purpose of that variable
    (note it's a global variable, not a compile-time constant!) is so that
    code linked against earlier frameworks can see if it's running on a
    version of the OS older or newer than the SDK it was linked against.

    The frameworks can detect when you've linked against a different version
    of AppKit than you're running on, and will often change their behavior
    accordingly.

    --Kyle Sluder
  • On Jul 6, 2012, at 1:01 PM, Stephen J. Butler wrote:

    > On Fri, Jul 6, 2012 at 4:35 AM, ecir hana <ecir.hana...> wrote:
    >> Sorry I should've said that before: no, I'm on 10.6.
    >>
    >> But thanks for the reply!
    >
    > You're positive you're linking against the 10.6 SDK? Even if you're on
    > 10.6 you might be linking against the 10.5 SDK and that would explain
    > your problem perfectly.

    It wouldn't matter.  If you have the "class-dump" tool, you can use it on the 10.6 or 10.7 AppKit framework.  You'll see that most of the Cocoa protocols are present, but NSApplicationDelegate isn't.

    On Jul 6, 2012, at 5:09 AM, ecir hana wrote:

    > On Fri, Jul 6, 2012 at 11:56 AM, Ken Thomases <ken...> wrote:
    >
    >>> Is there a way to get NSApplicationDelegate protocol besides the
    >>> compile-time "@protocol()"?
    >>
    >> No.  The run-time information about the protocol is obtained from
    >> information that would have to have been baked into the executable at build
    >> time.
    >>
    >>
    >>> Also, when I do:
    >>>
    >>> Protocol *protocol = objc_getProtocol("NSTextViewDelegate");
    >>>
    >>> return the protocol even when I didn't create any textviews...?
    >>
    >> You have presumably loaded AppKit, and AppKit does have the protocol
    >> details for NSTextViewDelegate baked into it, because it includes classes
    >> which adopt it and/or code which uses @protocol(NSTextViewDelegate).
    >>
    >
    > Thank you. And please, can you explain to me why is it that case? Is it simply because AppKit includes the definition of NSTextViewDelegate protocol and not includes one for NSApplicationDelegate?

    Correct.

    > Is it somewhere documented which protocols are not included?

    I doubt it.  It's not that anybody made a conscious decision to omit NSApplicationDelegate.  It's just a consequence of how the code was written and what the compiler and linker do.

    > I mean, besides NSApplicationDelegate, what other delegate definitions cannot be obtained at runtime?

    Who knows.  It depends entirely on what the AppKit code used and what the compiler and linker did with that.

    > Or is there anything like AppKit which, after being loaded, provides the definitions?

    Anything which referenced them in the described manner (includes a class which adopted them or code which references them via @protocol()).

    Regards,
    Ken
  • On Sat, Jul 7, 2012 at 6:30 AM, Ken Thomases <ken...> wrote:

    >>
    >> Thank you. And please, can you explain to me why is it that case? Is it
    > simply because AppKit includes the definition of NSTextViewDelegate
    > protocol and not includes one for NSApplicationDelegate?
    >
    > Correct.
    >
    >> Is it somewhere documented which protocols are not included?
    >
    > I doubt it.  It's not that anybody made a conscious decision to omit
    > NSApplicationDelegate.  It's just a consequence of how the code was written
    > and what the compiler and linker do.
    >
    >> I mean, besides NSApplicationDelegate, what other delegate definitions
    > cannot be obtained at runtime?
    >
    > Who knows.  It depends entirely on what the AppKit code used and what the
    > compiler and linker did with that.
    >
    >> Or is there anything like AppKit which, after being loaded, provides the
    > definitions?
    >
    > Anything which referenced them in the described manner (includes a class
    > which adopted them or code which references them via @protocol()).
    >
    >
    Thank you so much for the explanation!
previous month july 2012 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