[swift-users] Referencing Swift Functions from the Objective-C Runtime

Jacob Bandes-Storch jtbandes at gmail.com
Sun Nov 20 23:08:29 CST 2016


For a function such as bar() above, the type you want to cast the IMP to
would probably be "@convention(c) (Foo, Selector) -> ()".

On Sun, Nov 20, 2016 at 9:05 PM, Jeff Kelley <slaunchaman at gmail.com> wrote:

> 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.
>
>
> Jeff Kelley
>
> SlaunchaMan at gmail.com | @SlaunchaMan <https://twitter.com/SlaunchaMan> |
> jeffkelley.org
>
> On Nov 19, 2016, at 1:58 AM, Jacob Bandes-Storch <jtbandes at gmail.com>
> wrote:
>
> 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?
>
> Jacob
>
> On Fri, Nov 18, 2016 at 10:37 PM, Jeff Kelley via swift-users <
> swift-users at swift.org> wrote:
>
>> Hello,
>>
>> 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:
>>
>> class Foo: SomeSuperclass {
>>
>>     @objc func bar() {
>>         print("Hello, World!")
>>     }
>>
>> }
>>
>> Using the Objective-C runtime methods, I can get the method with
>> class_copyMethodList and then get to the method’s implementation using
>> method_getImplementation. However, what I need to do next is to stick
>> this into a tuple that looks like this:
>>
>> typealias FooEntry = (fooClass: SomeSuperclass.Type, methods: [(String, (
>> Foo) throws -> Void)])
>>
>> For now, the workaround is to make a static variable that returns all of
>> the entries:
>>
>>     static var allEntries = {
>>         return [
>>             ("bar", bar),
>>         ]
>>     }
>>
>> Is there any way to go from the raw IMP that I get back from the runtime
>> to the Swift type so I can construct this list dynamically?
>>
>>
>> Jeff Kelley
>>
>> SlaunchaMan at gmail.com | @SlaunchaMan <https://twitter.com/SlaunchaMan> |
>> jeffkelley.org
>>
>>
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-users
>>
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161120/0cde1b0f/attachment.html>


More information about the swift-users mailing list