<div dir="ltr">For a function such as bar() above, the type you want to cast the IMP to would probably be &quot;@convention(c) (Foo, Selector) -&gt; ()&quot;.<div class="gmail_extra">
<br><div class="gmail_quote">On Sun, Nov 20, 2016 at 9:05 PM, Jeff Kelley <span dir="ltr">&lt;<a href="mailto:slaunchaman@gmail.com" target="_blank">slaunchaman@gmail.com</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">Thanks Jacob! I tried using unsafeBitCast, but it fails with the following: “fatal error: can&#39;t unsafeBitCast between types of different sizes”. I considered wrapping every call in a closure that calls objc_msgSend(), but alas, that’s not exposed to Swift. I have another approach in mind, so I’ll try that next.<div><br></div><div><span class=""><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_-5343275286513492557Apple-style-span" style="border-collapse:separate;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;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></span><div><div class="h5"><div><blockquote type="cite"><div>On Nov 19, 2016, at 1:58 AM, Jacob Bandes-Storch &lt;<a href="mailto:jtbandes@gmail.com" target="_blank">jtbandes@gmail.com</a>&gt; wrote:</div><br class="m_-5343275286513492557Apple-interchange-newline"><div><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="m_-5343275286513492557gmail_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_-5343275286513492557m_-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><div 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_-5343275286513492557m_-7480237009779366933webkit-block-placeholder"></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">@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">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#3495af">print</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures">&quot;Hello, World!&quot;</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 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_-5343275286513492557m_-7480237009779366933webkit-block-placeholder"></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><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><br></div><div><span class="m_-5343275286513492557m_-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_-5343275286513492557m_-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_-5343275286513492557m_-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="letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div style="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="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="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="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_-5343275286513492557m_-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">@Slaun<wbr>chaMan</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" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-users</a><br>
<br></blockquote></div><br></div></div>
</div></blockquote></div><br></div></div></div></div></blockquote></div><br></div></div>