5

I'm trying to use the objc_getProtocol() function to get a reference to the struct representing the NSApplicationDelegate protocol:

Protocol *protocol = objc_getProtocol("NSApplicationDelegate");

However, for some reason, this always returns NULL.

Other protocols such as NSObject, NSCoding, NSTableViewDelegate, and NSTableViewDataSource work fine.

Is there something special about NSApplicationDelegate, or am I doing something wrong?

jscs
  • 63,694
  • 13
  • 151
  • 195
Greg Brown
  • 3,168
  • 1
  • 27
  • 37
  • is it mac application? If it is then pls add that tag! coz I dont find NSApplicationDelegate in iOS(here we hv UIApplicationDelegate) Application.!? Pls correct me if i m wrong.! – hp iOS Coder Apr 18 '12 at 15:09
  • 1
    Yes, it is a Mac application (hence the AppKit tag). – Greg Brown Apr 18 '12 at 17:10
  • And what about _NSApplicationLightLaunchDelegate? – fbernardo Apr 18 '12 at 19:02
  • What happens if you call `objc_copyProtocolList()` to get a list of all protocols known to the runtime? – Rob Keniger Apr 19 '12 at 04:37
  • @fbernado - yes, _NSApplicationLightLaunchDelegate works. – Greg Brown Apr 19 '12 at 20:22
  • @RobKeniger - I get a list of 177 protocols, but (for whatever reason) NSApplicationDelegate is not one of them. _NSApplicationLightLaunchDelegate is included in the list. – Greg Brown Apr 19 '12 at 20:23
  • That *is* weird. Apple must be doing some sort of protocol-swizzling (which I didn't even think was possible) to have the `_NSApplicationLightLaunchDelegate` protocol masquerade as `NSApplicationDelegate`. It's odd, because `NSApplication.h` explicitly declares the protocol so it should be there. I hope you find an answer. – Rob Keniger Apr 19 '12 at 22:39
  • You can use `@protocol(NSApplicationDelegate)` to get that. – hypercrypt Apr 18 '12 at 18:46
  • Unfortunately I need to use objc_getProtocol(), since I don't actually know the protocol name until runtime (I was just hard-coding the string in the example). – Greg Brown Apr 18 '12 at 19:47
  • Of course, I could use this as a workaround...thanks for the suggestion! – Greg Brown Apr 18 '12 at 19:48

1 Answers1

2

Found the answer in the Apple docs:

http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Conceptual/ObjectiveC/Chapters/ocProtocols.html#//apple_ref/doc/uid/TP30001163-CH15

The compiler creates a protocol object for each protocol declaration it encounters, but only if the protocol is also:

  • Adopted by a class,
  • Or referred to somewhere in source code (using @protocol())
Greg Brown
  • 3,168
  • 1
  • 27
  • 37