0

I, too, am trying to:

Protocol *protocol = objc_getProtocol("NSApplicationDelegate");

as seen here:

objc_getProtocol() returns NULL for `NSApplicationDelegate"?

The accepted answer says that it is returning NULL because I haven't done one of:

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

Now, @protocol is out of question because I run this at runtime, right? So that leaves me with adding the protocol to the class, with BOOL class_addProtocol(Class cls, Protocol *protocol)

But how can I add a protocol to a class when the protocol was not yet created?

In other words, it seems I have to do:

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

But the first line returns NULL...

Community
  • 1
  • 1
Ecir Hana
  • 10,864
  • 13
  • 67
  • 117

2 Answers2

0

The answer to this "part 2" question is the same as the answer to your original question. The documentation says:

Protocols that are declared but not used (except for type checking as described below) aren’t represented by protocol objects at runtime.

You can't get the Protocol because it doesn't exist.

Tom Dalling
  • 23,305
  • 6
  • 62
  • 80
  • Just a second - I would like to know, at runtime, what methods a protocol specifies. Are you saying that for `NSApplicationDelegate` this is not possible? – Ecir Hana Jul 06 '12 at 08:13
  • Btw, I can get `objc_getProtocol("NSTextViewDelegate");` even when I didn't create any textviews...? – Ecir Hana Jul 06 '12 at 08:14
  • If `NSApplicationDeletegate` is never used anywhere in the code, then not `Protocol` object is made for it. Write a class that implements the protocol, like `@interface MyAppDelegate` and the `Protocol` object will be made. – Tom Dalling Jul 06 '12 at 13:59
0

Seems like your best bet at the moment is to parse the BridgeSupport files and get the method types you want from the <informal_protocol> tag.

NSApplicationDelegate is described in AppKit.bridgesupport:

<informal_protocol name='NSApplicationDelegate'>
<method selector='application:didDecodeRestorableState:' type='v16@0:4@8@12' type64='v32@0:8@16@24'/>
<method selector='application:didFailToRegisterForRemoteNotificationsWithError:' type='v16@0:4@8@12' type64='v32@0:8@16@24'/>
<method selector='application:didReceiveRemoteNotification:' type='v16@0:4@8@12' type64='v32@0:8@16@24'/>
<method selector='application:didRegisterForRemoteNotificationsWithDeviceToken:' type='v16@0:4@8@12' type64='v32@0:8@16@24'/>
<method selector='application:openFile:' type='B16@0:4@8@12' type64='B32@0:8@16@24'/>
<method selector='application:openFileWithoutUI:' type='B16@0:4@8@12' type64='B32@0:8@16@24'/>
<method selector='application:openFiles:' type='v16@0:4@8@12' type64='v32@0:8@16@24'/>
<method selector='application:openTempFile:' type='B16@0:4@8@12' type64='B32@0:8@16@24'/>
<method selector='application:printFile:' type='B16@0:4@8@12' type64='B32@0:8@16@24'/>
<method selector='application:printFiles:withSettings:showPrintPanels:' type='I24@0:4@8@12@16B20' type64='Q44@0:8@16@24@32B40'/>
<method selector='application:willEncodeRestorableState:' type='v16@0:4@8@12' type64='v32@0:8@16@24'/>
<method selector='application:willPresentError:' type='@16@0:4@8@12' type64='@32@0:8@16@24'/>
<method selector='applicationDidBecomeActive:' type='v12@0:4@8' type64='v24@0:8@16'/>
<method selector='applicationDidChangeOcclusionState:' type='v12@0:4@8' type64='v24@0:8@16'/>
<method selector='applicationDidChangeScreenParameters:' type='v12@0:4@8' type64='v24@0:8@16'/>
<method selector='applicationDidFinishLaunching:' type='v12@0:4@8' type64='v24@0:8@16'/>
<method selector='applicationDidHide:' type='v12@0:4@8' type64='v24@0:8@16'/>
<method selector='applicationDidResignActive:' type='v12@0:4@8' type64='v24@0:8@16'/>
<method selector='applicationDidUnhide:' type='v12@0:4@8' type64='v24@0:8@16'/>
<method selector='applicationDidUpdate:' type='v12@0:4@8' type64='v24@0:8@16'/>
<method selector='applicationDockMenu:' type='@12@0:4@8' type64='@24@0:8@16'/>
<method selector='applicationOpenUntitledFile:' type='B12@0:4@8' type64='B24@0:8@16'/>
<method selector='applicationShouldHandleReopen:hasVisibleWindows:' type='B16@0:4@8B12' type64='B28@0:8@16B24'/>
<method selector='applicationShouldOpenUntitledFile:' type='B12@0:4@8' type64='B24@0:8@16'/>
<method selector='applicationShouldTerminate:' type='I12@0:4@8' type64='Q24@0:8@16'/>
<method selector='applicationShouldTerminateAfterLastWindowClosed:' type='B12@0:4@8' type64='B24@0:8@16'/>
<method selector='applicationWillBecomeActive:' type='v12@0:4@8' type64='v24@0:8@16'/>
<method selector='applicationWillFinishLaunching:' type='v12@0:4@8' type64='v24@0:8@16'/>
<method selector='applicationWillHide:' type='v12@0:4@8' type64='v24@0:8@16'/>
<method selector='applicationWillResignActive:' type='v12@0:4@8' type64='v24@0:8@16'/>
<method selector='applicationWillTerminate:' type='v12@0:4@8' type64='v24@0:8@16'/>
<method selector='applicationWillUnhide:' type='v12@0:4@8' type64='v24@0:8@16'/>
<method selector='applicationWillUpdate:' type='v12@0:4@8' type64='v24@0:8@16'/>
</informal_protocol>