Home » Swift » Is it possible to write inline assembly in Swift?

Is it possible to write inline assembly in Swift?

Posted by: admin November 30, 2017 Leave a comment

Questions:

I was wondering if you can write inline assembly in Swift.

I know that in Objective-C you could use something like this:

inline void assemblyFunc() {
    __asm__(/*Assembly*/);
}

But in Swift it seems that you can’t use __asm__(/*Assembly*/).

Does anyone know how to use __asm__() if its even possible.
I haven’t found anything about it, so I thought it would be a good question to ask.

Answers:

To expand on what Robert Levy said, you can just use the Swift/Obj-C interop feature, and write an Obj-C class that does the ASM stuff, which you can then call from Swift.

It’s an annoying workaround, but it should work nonetheless.

You can read more about how to do it [here]

Questions:
Answers:

There isn’t a way in Swift itself. If you need this, probably a good opportunity to take advantage of Swift-ObjC interop.