Swift with C struct pointer
我有一个包含指针的C结构,全部在C头中定义:
struct testA { int iSignal; struct testB *test; }; struct testB { int iPro; };
然后我有一个Swift程序来初始化这个结构的一个实例:
var a = testA() var b = testB() a.test = &b // this line error
但是我收到以下错误:
'&' used with non-inout argument of type 'UnsafeMutablePointer'
有人可以帮忙吗?
快速文件
import Foundation var s = testA() s.iSignal = 1 s.test = UnsafeMutablePointer.alloc(1) // alocate memory for one instance of testB s.test.memory.iPro = 10 // if your testB is allocated in you C code, than just access it let testB = UnsafeMutablePointer (s.test) dump(testB.memory) /* ▿ __C.testB - iPro: 10 */ dump(s) /* ▿ __C.testA - iSignal: 1 ▿ test: UnsafeMutablePointer(0x1007009C0) - pointerValue: 4302309824 */ dump(s.test.memory) /* ▿ __C.testB - iPro: 10 */ s.test.dealloc(1) // dont forget deallocate s.test underlying memory !
mystruct.h
#ifndef mystruct_h #define mystruct_h struct testA { int iSignal; struct testB *test; }; struct testB { int iPro; }; #endif /* mystruct_h */
TESTC-Bridgin-Header.h
#include "mystruct.h"