<div dir="ltr">"throws" is the part that's not representable in Obj-C. Why are you using it? If you're interacting with method_getImplementation, you need to think like the Obj-C runtime.<div><br></div><div><a href="https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtHowMessagingWorks.html#//apple_ref/doc/uid/TP40008048-CH104-SW1">https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtHowMessagingWorks.html#//apple_ref/doc/uid/TP40008048-CH104-SW1</a><br><div><br></div><div>This works:</div><div><br></div><div> typealias DescriptionMethod = @convention(c) (NSObject, Selector) -> NSString </div><div><br></div><div> let fn = unsafeBitCast(method_getImplementation(class_getInstanceMethod(NSObject.self, "description")), DescriptionMethod.self)</div><div><br></div><div> fn(NSObject(), "description") as String</div><div><br></div><div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr"><div>Jacob<br></div></div></div></div>
<br><div class="gmail_quote">On Sun, Nov 20, 2016 at 9:41 PM, Jeff Kelley <span dir="ltr"><<a href="mailto:slaunchaman@gmail.com" target="_blank">slaunchaman@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word">Still trying on this (copied the code directly, Foo is actually XCTestCase):<div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(4,51,255)"><span style="font-variant-ligatures:no-common-ligatures">typealias</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> TestMethod = </span><span style="font-variant-ligatures:no-common-ligatures">@convention</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(c) (</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">XCTestCase</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">) </span><span style="font-variant-ligatures:no-common-ligatures">throws</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> -> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">Void</span></div></div><div><br></div><div>This seagulls the compiler with “<span style="font-family:menlo;font-size:11px">error: '(XCTestCase) throws -> Void' is not representable in Objective-C, so it cannot be used with '@convention(c)</span><font face="Menlo"><span style="font-size:11px">’</span></font>”. I’m trying to use it here:</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:rgb(4,51,255)">let</span><span style="font-variant-ligatures:no-common-ligatures"> testMethod: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">IMP</span><span style="font-variant-ligatures:no-common-ligatures"> = method_getImplementation(<wbr>method)</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></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">let</span><span style="font-variant-ligatures:no-common-ligatures"> test: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">TestMethod</span><span style="font-variant-ligatures:no-common-ligatures"> = unsafeBitCast(testMethod,</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures"> to: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">TestMethod</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">self</span><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></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">testMethods.append((methodName </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">as</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">String</span><span style="font-variant-ligatures:no-common-ligatures">, test))</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>If I try to put the type directly in the call to unsafeBitCast(), the compiler gives me an error:</div><div><br></div><div><div style="margin:0px 0px 0px 12px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px">Attribute can only be applied to types, not declarations<b></b></div><div style="margin:0px 0px 0px 12px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><br></div></div><div>Thanks for your suggestions! I hadn’t seen @convention() before.</div><div><br></div><div><span class="gmail-"><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: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: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: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: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="gmail-m_9151264134778263134Apple-style-span" style="border-collapse:separate;font-variant-ligatures:normal;font-variant-numeric:normal;line-height:normal"><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="gmail-h5"><div><blockquote type="cite"><div>On Nov 21, 2016, at 12:08 AM, Jacob Bandes-Storch <<a href="mailto:jtbandes@gmail.com" target="_blank">jtbandes@gmail.com</a>> wrote:</div><br class="gmail-m_9151264134778263134Apple-interchange-newline"><div><div dir="ltr">For a function such as bar() above, the type you want to cast the IMP to would probably be "@convention(c) (Foo, Selector) -> ()".<div class="gmail_extra">
<br><div class="gmail_quote">On Sun, Nov 20, 2016 at 9:05 PM, Jeff Kelley <span dir="ltr"><<a href="mailto:slaunchaman@gmail.com" target="_blank">slaunchaman@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word">Thanks Jacob! I tried using unsafeBitCast, but it fails with the following: “fatal error: can'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><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: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: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: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: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="gmail-m_9151264134778263134m_-5343275286513492557Apple-style-span" style="border-collapse:separate;font-family:helvetica;font-style:normal;font-variant: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"><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></span><div><div class="gmail-m_9151264134778263134h5"><div><blockquote type="cite"><div>On Nov 19, 2016, at 1:58 AM, Jacob Bandes-Storch <<a href="mailto:jtbandes@gmail.com" target="_blank">jtbandes@gmail.com</a>> wrote:</div><br class="gmail-m_9151264134778263134m_-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 "(Foo) throws -> Void" ? Or do you somehow want to dynamically use the type information?<div class="gmail_extra"><br clear="all"><div><div class="gmail-m_9151264134778263134m_-5343275286513492557gmail_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"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word">Hello,<div><br></div><div><span class="gmail-m_9151264134778263134m_-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:rgb(4,51,255)">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="gmail-m_9151264134778263134m_-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:rgb(4,51,255)">@objc</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">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:rgb(52,149,175)">print</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures">"Hello, World!"</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="gmail-m_9151264134778263134m_-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="gmail-m_9151264134778263134m_-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:rgb(4,51,255)">typealias</span><span style="font-variant-ligatures:no-common-ligatures"> FooEntry = (fooClass: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">SomeSuperclass</span><span style="font-variant-ligatures:no-common-ligatures">.Type, methods: [(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">String</span><span style="font-variant-ligatures:no-common-ligatures">, (</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">Foo</span><span style="font-variant-ligatures:no-common-ligatures">) </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">throws</span><span style="font-variant-ligatures:no-common-ligatures"> -> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">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="gmail-m_9151264134778263134m_-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:rgb(4,51,255)">static</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">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:rgb(4,51,255)">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:rgb(180,38,26)">"bar"</span><span style="font-variant-ligatures:no-common-ligatures">, </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">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="gmail-m_9151264134778263134m_-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: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: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: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: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="gmail-m_9151264134778263134m_-5343275286513492557m_-7480237009779366933Apple-style-span" style="border-collapse:separate;font-variant-ligatures:normal;font-variant-numeric:normal;line-height:normal"><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>
</div></blockquote></div><br></div></div></div></div></blockquote></div><br></div></div></div></div>