Access C variable in swift

岁酱吖の 提交于 2019-11-29 15:52:37

The problem is that struct g722_dstate is an "incomplete type", and Swift cannot import variables of an incomplete type, only variables which are pointers to an incomplete type (and those are imported as OpaquePointer).

Adding the complete struct definition to the imported header file would be the easiest solution.

If that is not possible then one workaround would be to add

#import "g722_codec.h"

static struct g722_dstate * __nonnull dstatePtr = &dstate;

to the bridging header file, which defines a variable containing the address of the "opaque" dstate variable. This is imported to Swift as

var dstatePtr: OpaquePointer

and can then be used e.g. as

g722_coder_init(dstatePtr)

You can do like create a function in c file and Add function name as forward declaration in bridging header For example -

//In c File you have definition & forward declaration
int g722_encode(short *data, unsigned char *outdata,int len, struct g722_cstate *s  );
int g722_encode(short *data, unsigned char *outdata,int len, struct g722_cstate *s  ){

}

Bridging header should be -

 #import "g722_codec.h"
int g722_encode(short *data, unsigned char *outdata,int len, struct g722_cstate *s  );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!