22

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.

8
  • 1
    There's no inline asm support. Please report a bug if this feature is important to you. Commented Jun 16, 2014 at 15:03
  • 1
    the Swift is mostly designed for the entry-level developers on iOS platform. Commented Jun 16, 2014 at 15:04
  • @holex I don't think so. Swift is designed to be more expressive and make developers more productive in the cases where performance is not the number 1 priority, which is most of them Commented Jun 16, 2014 at 15:16
  • 6
    Nowhere in Apple's developer or marketing materials for Swift do they say it's only or primarily for new developers. As @Jiaaro notes, it's designed to increase productivity for all developers. (And it's designed to be fast, even if it's not always so in the beta.) Commented Jun 16, 2014 at 15:20
  • 1
    Possible duplicate of calling a function defined in assembly from swift Commented Oct 23, 2019 at 1:26

2 Answers 2

14

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 ObjC-to-Swift interop here

Sign up to request clarification or add additional context in comments.

4 Comments

Are you just repeating Robert Levy's answer?
No, he's building on it. Robert didn't exactly explain what to do, just to "take advantage" of a feature. Electric Coffee actually explained the solution, what steps the questioner should take.
@milesper the "explanation" being "write an ObjC class"? Darn, I forgot to include that clarification
I've further expanded my answer a bit
14

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

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.