斯威夫特:通过未初始化的C结构来导入C函数 [英] Swift: Pass Uninitialized C Structure to Imported C function

查看:127
本文介绍了斯威夫特:通过未初始化的C结构来导入C函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所知道的<一个href=\"http://stackoverflow.com/questions/24090148/c-style-uninitialized-pointer-passing-in-apple-swift\">this回答时,但是这是不一样的东西。 - 这就是指针传递到与分配被初始化

I'm aware of this answer, but this is not the same thing - thats passing a pointer to be initialised with an allocation.

我与C库具有以下结构定义接口

I'm interfacing with a C library that has the following structure definition:

typedef struct myStruct { unsigned char var [50]; } myStruct;

有对你传递结构的地址的功能 - 通常基于栈堆没有,这样的:

There is a function to which you pass the address of the structure - normally stack based not heap, thus:

myStruct mine;
initMyStruct(&mine);

这初始化该结构的内容 - 调用方不知道内存的内部格式,因此混淆

This initialises the content of the struct - the caller does not know the internal format of the memory, hence the obfuscation.

在斯威夫特,我创建一个类,将封装结构和接口与它经营其他的C函数。

In Swift, I'm creating a class which will encapsulate the structure and interface with other C functions which operates on it.

class MyClass {

    var mine : myStruct

    init() {

        initMyStruct(&mine)
        // Error: Variable 'self.msg' passed by reference before being initialized

    }

}

我不能为我的生活工作如何初始化结构,或者用一个点来代替,如果这是一种选择。

I can't for the life of me work out how to initialise the structure, or use a point instead if that is an alternative.

mine = myStruct()
// Fails because you aren't providing the value of the member 'var'

mine = myStruct((CUnsignedChar(), CUnsignedChar(), /*... repeat to 50 */))
// Cannot find an overload for 'init' that accepts the arguments

请注意,这是传递一个已分配(栈为基础)结构的地址已被导入为

Please note, this is passing the address of an already allocated (stack based) structure to a function that has been imported as

CInt initMyStruct(str: CMutablePointer<myStruct>)

这不是一个指针传递给C库中的问题,这似乎是一个比较常见的要求,在别处回答进行分配。

It is NOT passing a pointer to be allocated by the C library in question which seems to be a more common requirement and is answered elsewhere.

目前雨燕不支持固定大小的数组无论是。

Swift currently doesn't support fixed size arrays either.

我看不到如何满足结构的初始化或作出斯威夫特认为这是将要被调用,这样完成的。

I can't see how to satisfy the initialisation of the structure or make Swift think that it is going to be done so by the call.

任何帮助非常AP preciated!

Any help greatly appreciated!

推荐答案

首先,我们来定义我们的C code来进行测试:

First, let's define our C code for testing:

typedef struct my_struct {
    unsigned char buffer[10];
} my_struct;

void my_struct_init(my_struct *my_s) {
    for (int i = 0; i < 10; i++) {
        my_s->buffer[i] = (char) i;
    }
}

在斯威夫特,我们有两个选择:

In Swift, we have two options:

1。栈上结构

var my_s: my_struct = ...

不过,我们必须以某种方式初始化。每个结构都有一个默认的初始

However, we have to initialize it somehow. Every struct has a default initializer

var my_s: my_struct = my_struct(buffer: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0))

请注意,在这种情况下,缓冲[10] 已被翻译为雨燕作为 10元组

Note that in this case the buffer[10] has been translated to Swift as a 10-tuple.

现在,我们可以调用:

my_struct_init(&my_s)
println("Buffer: \(my_s.buffer)") // Buffer: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

然而,更复杂的是该结构中,更困难的是使用默认初始化

However, the more complex is the struct, the more difficult is to use the default initializer.

2。堆结构

这是类似于C使用的malloc 免费

var my_s_pointer = UnsafeMutablePointer<my_struct>.alloc(1)
println("Buffer: \(my_s.buffer)") // Buffer: (some random values)

my_struct_init(my_s_pointer)
println("Buffer: \(my_s_pointer.memory.buffer)") // Buffer: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

my_s_pointer.destroy()

联合两种​​方法

下面的函数将初始化任何结构:

The following function will initialize any struct:

func initStruct<S>() -> S {
    let struct_pointer = UnsafeMutablePointer<S>.alloc(1)

    let struct_memory = struct_pointer.memory
    struct_pointer.destroy()

    return struct_memory
}

var my_s: my_struct = initStruct()
my_struct_init(&my_s)

这篇关于斯威夫特:通过未初始化的C结构来导入C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆