简体   繁体   English

快速访问 C 结构体

[英]Swift access to C Struct

I am developing a OS X Swift app for parsing cvs files.我正在开发一个用于解析 cvs 文件的 OS X Swift 应用程序。 It runs successfully in Objective-C.它在 Objective-C 中成功运行。 Then I changed to Swift and for performance improvements I developed the parse/import engine in C. It is 5 times faster as in Swift or Objective-C - nice.然后我改用 Swift,为了性能改进,我用 C 开发了解析/导入引擎。它比 Swift 或 Objective-C 快 5 倍 - 很好。 But I have trouble to exchange the data between C and Swift - especially with Struct:但是我很难在 C 和 Swift 之间交换数据——尤其是使用 Struct:

BridgingHeader:桥接头:

#include "ToolBoxC.h"

ToolBoxC.h:工具箱C.h:

void loadFile(const char *fileName, const char *delimiters, const char *xRegex, int xRegexColumn, int xColumn, int yColumn, int xRow, int yRowShift, bool collectStrings);

typedef struct {

                    char **headerArray;
                    int numberHeaderRows;
                    char **dateArray;
                    int numberDateRows;
                    int **valueArray;
                    char ***stringArray;
                    int numberValueRows;
                    int numberValueColums;
        } FileStruct;

typedef struct {

                    FileStruct fileContent[10000];
} FilesStruct;

struct FilesStruct filesContent;

ToolBoxC.c:工具箱C.c:

struct FileStruct {

    char **headerArray;
    int numberHeaderRows;
    char **dateArray;
    int numberDateRows;
    int **valueArray;
    char ***stringArray;
    int numberValueRows;
    int numberValueColums;
};

struct FilesStruct {

    struct FileStruct fileContent[10000];
};

void loadFile(const char *fileName, const char *delimiters, const char *xRegex, int xRegexColumn, int xColumn, int yColumn, int xRow, int yRowShift, bool collectStrings) {

// some stuff

    struct FileStruct fileContent;

    fileContent.headerArray = headerArray;
    fileContent.numberHeaderRows = numberHeaderRows;
    fileContent.dateArray = dateArray;
    fileContent.numberDateRows = numberDateRows;
    fileContent.valueArray = valueArray;
    fileContent.stringArray = stringArray;
    fileContent.numberValueRows = numberValueRows;
    fileContent.numberValueColums = numberValueColumns;

    filesContent.fileContent[numberFiles] = fileContent;

return;
}

All the parsed data are stored in struct FilesStruct filesContent .所有解析的数据都存储在struct FilesStruct filesContent The parsing is started by calling the function loadFile() with parameters from Swift.解析是通过调用带有 Swift 参数的函数 loadFile() 开始的。 That works fine.这很好用。 Also the parsing is OK.解析也OK。 But how can I access to the data in struct FilesStruct filesContent from Swift?但是如何从 Swift 访问struct FilesStruct filesContent的数据?

Thanks, Matthias.谢谢,马蒂亚斯。

Try this:尝试这个:

ToolBoxC.h工具箱C.h

#include <stdbool.h>

struct FileStruct {
    char **headerArray;
    int numberHeaderRows;
    char **dateArray;
    int numberDateRows;
    int **valueArray;
    char ***stringArray;
    int numberValueRows;
    int numberValueColums;
};

extern struct FileStruct **loadedFiles;

void loadFile(const char *fileName, const char *delimiters, const char *xRegex, int xRegexColumn, int xColumn, int yColumn, int xRow, int yRowShift, bool collectStrings);

ToolBoxC.c工具箱C.c

#include <stdlib.h>
#include "ToolBoxC.h"

#define MaxFiles 10000

struct FileStruct **loadedFiles;

void loadFile(const char *fileName, const char *delimiters, const char *xRegex, int xRegexColumn, int xColumn, int yColumn, int xRow, int yRowShift, bool collectStrings) {

    static int nextIndex = 0;

    if (loadedFiles == 0)
        loadedFiles = malloc(MaxFiles * sizeof(*loadedFiles));

    struct FileStruct *file = malloc(sizeof(struct FileStruct));
    file->numberDateRows = xRow;

    loadedFiles[nextIndex++] = file;
}

Swift Test Method Swift 测试方法

func loadFilesTest() -> Void {
    for var i:Int32 = 0; i < 10; ++i {
        loadFile("", "", "", 0, 0, 0, i, 0, true)
    }
    for var j = 0; j < 10; ++j {
        let pointer = UnsafePointer<FileStruct>(loadedFiles[j])
        print("Number of date rows = \(pointer.memory.numberDateRows)")
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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