繁体   English   中英

带有C结构指针的Swift

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM