0

我也想:

Protocol *protocol = objc_getProtocol("NSApplicationDelegate");

如此处所示:

objc_getProtocol() 为“NSApplicationDelegate”返回 NULL?

接受的答案说它返回 NULL 因为我还没有完成以下操作之一:

  • 被一个班级收养,
  • 或在源代码中的某处引用(使用@protocol()

现在,@protocol这是毫无疑问的,因为我在运行时运行它,对吧?所以这让我将协议添加到类中,BOOL class_addProtocol(Class cls, Protocol *protocol)

但是,当尚未创建协议时,如何将协议添加到类中?

换句话说,似乎我必须这样做:

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

但是第一行返回NULL ...

4

2 回答 2

0

这个“第 2 部分”问题的答案与原始问题的答案相同。文档说:

声明但未使用的协议(除了下面描述的类型检查)在运行时不由协议对象表示。

你不能得到,Protocol因为它不存在。

于 2012-07-06T07:54:05.687 回答
0

似乎您目前最好的选择是解析 BridgeSupport 文件并从<informal_protocol>标签中获取所需的方法类型。

NSApplicationDelegate 在 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>
于 2014-05-23T16:46:07.490 回答