[英]Swift with C struct pointer
我有一個包含指針的C結構,所有指針都定義在C標頭中:
struct testA {
int iSignal;
struct testB *test;
};
struct testB {
int iPro;
};
然后,我有一個Swift程序來初始化此struct的實例:
var a = testA()
var b = testB()
a.test = &b // this line error
但是我收到以下錯誤:
'&' used with non-inout argument of type 'UnsafeMutablePointer<testB>'
有人可以幫忙嗎?
快速文件
import Foundation
var s = testA()
s.iSignal = 1
s.test = UnsafeMutablePointer<testB>.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<testB>(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"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.