|
> It’s not recommend for new code (in Apple land) and they’re already starting to make features Swift-only. That's a real shame because FFI language bindings to Objective-C frameworks are amazingly simple to do with Objective-C. You basically create low-level C ABI bindings to objc_msgSend and about 40 other routines (e.g. class_getName). The latter bindings are principally for introspecting the class hierarchy and acquiring references to opaque class and method implementation pointers. method_getTypeEncoding returns a text-encoded type signature for a method. And objc_msgSend itself is a magical variable argument function for invoking arbitrary methods on arbitrary objects (including classes, which are objects themselves). With that small set of C ABI bindings you can generate strongly typed bindings to all the many thousands of Objective-C framework and library interfaces, system and third-party. I did this for Lua and it works beautifully. And you can just as easily define new classes and methods and wire them up to the host language (Lua, Python, Java, Rust, etc), though I haven't needed to do that just yet. In my case this is all done dynamically at runtime, except for the ~40 required static C function bindings. The only difficult part was binding objc_msgSend. You could use libffi, but in my case I wrote a script to statically generate a permutation of Lua/C binding functions with up to 6 arguments, any 2 of which could be doubles instead of scalars, and either a scalar or double return value. Atop those bindings sits a small Lua library which, using the other ~40 Lua/C function bindings, implements a type-safe bridge for the entire macOS Objective-C platform, GUI frameworks and all, modulo a small number of niche edge cases (e.g. IIRC there's a special Objective-C ABI for directly returning small structures which my objc_msgSend bindings don't implement, but I haven't run into those cases and my Lua bridge code would throw an error if I tried). Because Objective-C itself uses reference counting, it plays well with any GC environment or lack thereof. I'm sure Swift is a nice language, but there are many nice languages, and Objective-C makes language interoperability downright dreamy. |