我也在尝试:
Protocol *protocol = objc_getProtocol("NSApplicationDelegate");
如图所示:
objc_getProtocol() returns NULL for `NSApplicationDelegate"?
接受的答案是它返回NULL,因为我没有做过以下其中一个:
@protocol()
)现在,@protocol
是不可能的,因为我在运行时运行它,对吧?这样我就可以使用BOOL class_addProtocol(Class cls, Protocol *protocol)
但是,如果尚未创建协议,我怎样才能将协议添加到类中?
换句话说,似乎我必须这样做:
Protocol *protocol = objc_getProtocol("NSApplicationDelegate");
BOOL class_addProtocol(class, protocol);
但第一行返回NULL ...
答案 0 :(得分:0)
这个“第2部分”问题的答案与原始问题的答案相同。 The documentation说:
声明但未使用的协议(类型检查除外 如下所述)在运行时不由协议对象表示。
您无法获取Protocol
,因为它不存在。
答案 1 :(得分:0)
目前最好的办法是解析BridgeSupport文件并从<informal_protocol>
标记中获取所需的方法类型。
在AppKit.bridgesupport中描述了NSApplicationDelegate:
<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>