<div dir="ltr">I imagine unsafeBitCast would be the way to go here. But are you assuming that all of the instance methods have type &quot;(Foo) throws -&gt; Void&quot; ? Or do you somehow want to dynamically use the type information?<div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div>Jacob<br></div></div></div></div>
<br><div class="gmail_quote">On Fri, Nov 18, 2016 at 10:37 PM, Jeff Kelley via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Hello,<div><br></div><div><span class="m_-7480237009779366933Apple-tab-span" style="white-space:pre-wrap">        </span>I’m trying to enumerate the methods of a class in Swift using the Objective-C runtime. Everything is working fine so far, except for the very last step. Suppose I have a Swift class like this:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff">class</span><span style="font-variant-ligatures:no-common-ligatures"> Foo</span><span style="font-variant-ligatures:no-common-ligatures">: </span><span style="color:rgb(52,149,175);font-variant-ligatures:no-common-ligatures">SomeSuperclass</span> {</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span><br class="m_-7480237009779366933webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff">@objc</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff">func</span><span style="font-variant-ligatures:no-common-ligatures"> bar() {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(180,38,26)"><span style="font-variant-ligatures:no-common-ligatures;color:#000000">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#3495af">print</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">(</span><span style="font-variant-ligatures:no-common-ligatures">&quot;Hello, World!&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    }</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span><br class="m_-7480237009779366933webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><br></div><div><span class="m_-7480237009779366933Apple-tab-span" style="white-space:pre-wrap">        </span>Using the Objective-C runtime methods, I can get the method with <span style="color:rgb(52,149,175);font-family:Menlo;font-size:11px">class_copyMethodList</span> and then get to the method’s implementation using <span style="color:rgb(52,149,175);font-family:Menlo;font-size:11px">method_getImplementation</span><wbr>. However, what I need to do next is to stick this into a tuple that looks like this:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff">typealias</span><span style="font-variant-ligatures:no-common-ligatures"> FooEntry = (fooClass: </span><span style="font-variant-ligatures:no-common-ligatures;color:#3495af">SomeSuperclass</span><span style="font-variant-ligatures:no-common-ligatures">.Type, methods: [(</span><span style="font-variant-ligatures:no-common-ligatures;color:#3495af">String</span><span style="font-variant-ligatures:no-common-ligatures">, (</span><span style="font-variant-ligatures:no-common-ligatures;color:#3495af">Foo</span><span style="font-variant-ligatures:no-common-ligatures">) </span><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff">throws</span><span style="font-variant-ligatures:no-common-ligatures"> -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#3495af">Void</span><span style="font-variant-ligatures:no-common-ligatures">)])</span></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div><span class="m_-7480237009779366933Apple-tab-span" style="white-space:pre-wrap">        </span>For now, the workaround is to make a static variable that returns all of the entries:</div><div><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff">static</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff">var</span><span style="font-variant-ligatures:no-common-ligatures"> allEntries = {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff">return</span><span style="font-variant-ligatures:no-common-ligatures"> [</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">            (</span><span style="font-variant-ligatures:no-common-ligatures;color:#b4261a">&quot;bar&quot;</span><span style="font-variant-ligatures:no-common-ligatures">, </span><span style="font-variant-ligatures:no-common-ligatures;color:#3495af">bar</span><span style="font-variant-ligatures:no-common-ligatures">),</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        ]</span></div><div><span style="font-family:Menlo;font-size:11px">    </span><span style="font-family:Menlo;font-size:11px">}</span></div><div><br></div><div><span class="m_-7480237009779366933Apple-tab-span" style="white-space:pre-wrap">        </span>Is there any way to go from the raw <span style="color:rgb(52,149,175);font-family:Menlo;font-size:11px">IMP</span> that I get back from the runtime to the Swift type so I can construct this list dynamically?</div><div><br></div><div><br><div>
<div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div style="color:rgb(0,0,0);font-family:Helvetica;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-variant-alternates:normal;font-variant-east-asian:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div style="color:rgb(0,0,0);font-family:Helvetica;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-variant-alternates:normal;font-variant-east-asian:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div style="color:rgb(0,0,0);font-family:Helvetica;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-variant-alternates:normal;font-variant-east-asian:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div style="color:rgb(0,0,0);font-family:Helvetica;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-variant-alternates:normal;font-variant-east-asian:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><span class="m_-7480237009779366933Apple-style-span" style="border-collapse:separate;font-variant-ligatures:normal;font-variant-numeric:normal;font-variant-alternates:normal;font-variant-east-asian:normal;line-height:normal;border-spacing:0px"><div style="word-wrap:break-word"><div>Jeff Kelley</div><div><br></div><div><a href="mailto:SlaunchaMan@gmail.com" target="_blank">SlaunchaMan@gmail.com</a> | <a href="https://twitter.com/SlaunchaMan" target="_blank">@<wbr>SlaunchaMan</a> | <a href="http://jeffkelley.org" target="_blank">jeffkelley.org</a></div></div></span></div></div></div></div></div>
</div>

<br></div></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div></div>