7

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?

sam
  • 488
  • 8
  • 21
  • 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 Jun 11 '15 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 Jun 11 '15 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 Jun 11 '15 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. – Troy Harvey Jun 14 '15 at 03: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 Oct 25 '17 at 05:39

1 Answers1

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.

zneak
  • 134,922
  • 42
  • 253
  • 328