<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Still trying on this (copied the code directly, Foo is actually XCTestCase):<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(4, 51, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">typealias</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> TestMethod = </span><span style="font-variant-ligatures: no-common-ligatures" class="">@convention</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(c) (</span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">XCTestCase</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) </span><span style="font-variant-ligatures: no-common-ligatures" class="">throws</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Void</span></div></div><div class=""><br class=""></div><div style="text-indent: -12px;" class="">This seagulls the compiler with “<span style="font-family: Menlo; font-size: 11px; text-indent: -12px;" class="">error: '(XCTestCase) throws -> Void' is not representable in Objective-C, so it cannot be used with '@convention(c)</span><font face="Menlo" class=""><span style="font-size: 11px;" class="">’</span></font>”. I’m trying to use it here:</div><div style="text-indent: -12px;" class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> testMethod: </span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">IMP</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = method_getImplementation(method)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> test: </span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">TestMethod</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = unsafeBitCast(testMethod,</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> to: </span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">TestMethod</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">testMethods.append((methodName </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">as</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class="">, test))</span></div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class="">If I try to put the type directly in the call to unsafeBitCast(), the compiler gives me an error:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px 0px 0px 12px; text-indent: -12px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">Attribute can only be applied to types, not declarations<b class=""></b></div><div style="margin: 0px 0px 0px 12px; text-indent: -12px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class="">Thanks for your suggestions! I hadn’t seen @convention() before.</div><div class=""><br class=""></div><div class=""><br class=""><div class="">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant-ligatures: normal; font-variant-position: 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; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant-ligatures: normal; font-variant-position: 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; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant-ligatures: normal; font-variant-position: 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; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant-ligatures: normal; font-variant-position: 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; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; font-variant-ligatures: normal; font-variant-position: 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; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Jeff Kelley</div><div class=""><br class=""></div><div class=""><a href="mailto:SlaunchaMan@gmail.com" class="">SlaunchaMan@gmail.com</a> | <a href="https://twitter.com/SlaunchaMan" class="">@SlaunchaMan</a> | <a href="http://jeffkelley.org" class="">jeffkelley.org</a></div></div></span></div></div></div></div></div>
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On Nov 21, 2016, at 12:08 AM, Jacob Bandes-Storch <<a href="mailto:jtbandes@gmail.com" class="">jtbandes@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">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 class=""><div class="gmail_quote">On Sun, Nov 20, 2016 at 9:05 PM, Jeff Kelley <span dir="ltr" class=""><<a href="mailto:slaunchaman@gmail.com" target="_blank" class="">slaunchaman@gmail.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="">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 class=""><br class=""></div><div class=""><span class=""><br class=""><div class="">
<div style="letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; word-wrap: break-word;" class=""><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;" class=""><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;" class=""><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;" class=""><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;" class=""><span class="m_-5343275286513492557Apple-style-span" style="border-collapse: separate; 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" class=""><div class="">Jeff Kelley</div><div class=""><br class=""></div><div class=""><a href="mailto:SlaunchaMan@gmail.com" target="_blank" class="">SlaunchaMan@gmail.com</a> | <a href="https://twitter.com/SlaunchaMan" target="_blank" class="">@<wbr class="">SlaunchaMan</a> | <a href="http://jeffkelley.org/" target="_blank" class="">jeffkelley.org</a></div></div></span></div></div></div></div></div>
</div>
<br class=""></span><div class=""><div class="h5"><div class=""><blockquote type="cite" class=""><div class="">On Nov 19, 2016, at 1:58 AM, Jacob Bandes-Storch <<a href="mailto:jtbandes@gmail.com" target="_blank" class="">jtbandes@gmail.com</a>> wrote:</div><br class="m_-5343275286513492557Apple-interchange-newline"><div class=""><div dir="ltr" class="">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" class=""><div class=""><div class="m_-5343275286513492557gmail_signature" data-smartmail="gmail_signature"><div dir="ltr" class=""><div class="">Jacob<br class=""></div></div></div></div>
<br class=""><div class="gmail_quote">On Fri, Nov 18, 2016 at 10:37 PM, Jeff Kelley via swift-users <span dir="ltr" class=""><<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="">Hello,<div class=""><br class=""></div><div class=""><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 class=""><br class=""></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff" class="">class</span><span style="font-variant-ligatures:no-common-ligatures" class=""> Foo</span><span style="font-variant-ligatures:no-common-ligatures" class="">: </span><span style="color:rgb(52,149,175);font-variant-ligatures:no-common-ligatures" class="">SomeSuperclass</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><br class="m_-5343275286513492557m_-7480237009779366933webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff" class="">@objc</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff" class="">func</span><span style="font-variant-ligatures:no-common-ligatures" class=""> bar() {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(180,38,26)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#3495af" class="">print</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures" class="">"Hello, World!"</span><span style="font-variant-ligatures:no-common-ligatures" class="">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><br class="m_-5343275286513492557m_-7480237009779366933webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><br class=""></div><div class=""><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="">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" class="">method_getImplementation</span><wbr class="">. However, what I need to do next is to stick this into a tuple that looks like this:</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff" class="">typealias</span><span style="font-variant-ligatures:no-common-ligatures" class=""> FooEntry = (fooClass: </span><span style="font-variant-ligatures:no-common-ligatures;color:#3495af" class="">SomeSuperclass</span><span style="font-variant-ligatures:no-common-ligatures" class="">.Type, methods: [(</span><span style="font-variant-ligatures:no-common-ligatures;color:#3495af" class="">String</span><span style="font-variant-ligatures:no-common-ligatures" class="">, (</span><span style="font-variant-ligatures:no-common-ligatures;color:#3495af" class="">Foo</span><span style="font-variant-ligatures:no-common-ligatures" class="">) </span><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff" class="">throws</span><span style="font-variant-ligatures:no-common-ligatures" class=""> -> </span><span style="font-variant-ligatures:no-common-ligatures;color:#3495af" class="">Void</span><span style="font-variant-ligatures:no-common-ligatures" class="">)])</span></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div class=""><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 class=""><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff" class="">static</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff" class="">var</span><span style="font-variant-ligatures:no-common-ligatures" class=""> allEntries = {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff" class="">return</span><span style="font-variant-ligatures:no-common-ligatures" class=""> [</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> (</span><span style="font-variant-ligatures:no-common-ligatures;color:#b4261a" class="">"bar"</span><span style="font-variant-ligatures:no-common-ligatures" class="">, </span><span style="font-variant-ligatures:no-common-ligatures;color:#3495af" class="">bar</span><span style="font-variant-ligatures:no-common-ligatures" class="">),</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> ]</span></div><div class=""><span style="font-family:Menlo;font-size:11px" class=""> </span><span style="font-family:Menlo;font-size:11px" class="">}</span></div><div class=""><br class=""></div><div class=""><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" class="">IMP</span> that I get back from the runtime to the Swift type so I can construct this list dynamically?</div><div class=""><br class=""></div><div class=""><br class=""><div class="">
<div style="letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word" class=""><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" class=""><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" class=""><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" class=""><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" class=""><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" class=""><div class="">Jeff Kelley</div><div class=""><br class=""></div><div class=""><a href="mailto:SlaunchaMan@gmail.com" target="_blank" class="">SlaunchaMan@gmail.com</a> | <a href="https://twitter.com/SlaunchaMan" target="_blank" class="">@Slaun<wbr class="">chaMan</a> | <a href="http://jeffkelley.org/" target="_blank" class="">jeffkelley.org</a></div></div></span></div></div></div></div></div>
</div>
<br class=""></div></div><br class="">______________________________<wbr class="">_________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailma<wbr class="">n/listinfo/swift-users</a><br class="">
<br class=""></blockquote></div><br class=""></div></div>
</div></blockquote></div><br class=""></div></div></div></div></blockquote></div><br class=""></div></div>
</div></blockquote></div><br class=""></div></body></html>