8

I have seen many articles explaining how to call C functions from Swift, but I would like to explore the converse (if possible), to call Swift functions from C.

I have seen examples illustrating Swift functions in objC, but these do not get me any closer to my goal. Is this task even supported?

5
  • Just a guess, but no. You would need to setup the whole runtime for Swift from C which is no fun at all and will not make sense either. Calling a low level from a high level language makes sense for performance (or low level I/O) reasons. But vice versa it's simply - well I have no appropriate words if you know what I mean.
    – qwerty_so
    Commented Jun 11, 2015 at 20:32
  • yea, I would assume similarly. It would just be nice to extend existing projects written in C with new modules written in Swift if it were possible.
    – sam
    Commented Jun 11, 2015 at 20:36
  • Huh. As said: vice versa I would not have a problem but this way? Make a Swift scaffold and re-use your old C-code, step-wise replacing it by Swift code.
    – qwerty_so
    Commented Jun 11, 2015 at 20:38
  • As a way to start dipping into swift in a cautious approach, it is more attractive to start by writing just a sub-module in Swift under an existing C application, rather than rewrite top half of your C code base. Commented Jun 14, 2015 at 3:06
  • Hello there @sam I'm in a similar situation right now, have you found the correct way to do this? Some guideline would really be appreciated. Thanks!
    – Lysdexia
    Commented Oct 25, 2017 at 5:39

1 Answer 1

3

The only supported way to call Swift code from C code is to use Objective-C between the two.

That's not to say that it's entirely impossible, but someone would have to come up with a NSInvocation-like utility and wrappers for objects that don't have an exact C representation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.