Swift-ObjC Bridge Using @dynamicCallable
Of course Swift already has Objective-C integrated on the Apple platforms, directly supported by the compiler, as well as the associated bridging runtime. Yet using Dynamic Callable you can actually build something similar at the library level, and we want to show you how that would look like.
[…]
You may have wondered that
arrayByAddingObject:
instead ofaddObject:
was used to demo the thing. That had a reason 😜 Our signatures deal with methods returning object values, butaddObject:
is aVoid
method. If we invoke it, we crash, because ARC will attempt to release the non-existing result.
It’s a fun demo, but real-world bridging is complicated by memory management, error parameters and exceptions, non-object types, etc.
Previously: @dynamicCallable: Unix Tools as Swift Functions.