Home > AI > IOS > XCode >

How to call C code from Swift

Create a c file, XCode would ask you whether to generat the bridging file. You just need to say yes.

Then you will have three files in this example.

factorial.h
factorial.c
{ProjectName}-Bridging-Header.h

factorial.h

#ifndef factorial_h
#define factorial_h

#include <stdio.h>
long factorial(int n);

#endif /* factorial_h */

factorial.c

#include "factorial.h"


long factorial(int n) {
    if (n == 0 || n == 1) return 1;
    return n * factorial(n-1);
}

{ProjectName}-Bridging-Header.h

#include "factorial.h"

Leave a Reply