快速访问C结构

快速访问C结构,c,swift,cocoa,C,Swift,Cocoa,我正在开发一个OSXSwift应用程序,用于解析cvs文件。它在Objective-C中成功运行。然后我改为Swift,为了提高性能,我用C开发了解析/导入引擎。它比Swift或Objective-C-nice快5倍。但是我在C和Swift之间交换数据时遇到了麻烦-尤其是在使用Struct时: bridgengheader: #include "ToolBoxC.h" void loadFile(const char *fileName, const char *delimiters, con

我正在开发一个OSXSwift应用程序,用于解析cvs文件。它在Objective-C中成功运行。然后我改为Swift,为了提高性能,我用C开发了解析/导入引擎。它比Swift或Objective-C-nice快5倍。但是我在C和Swift之间交换数据时遇到了麻烦-尤其是在使用Struct时:

bridgengheader:

#include "ToolBoxC.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;
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;
}
ToolBoxC.h:

#include "ToolBoxC.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;
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;
}
ToolBoxC.c:

#include "ToolBoxC.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;
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;
}
所有解析的数据都存储在
struct filestruct filesContent
中。通过使用Swift中的参数调用函数loadFile()开始解析。那很好。解析也可以。但是如何从Swift访问
struct filestruct filesContent
中的数据

谢谢,马蒂亚斯。

试试这个:

ToolBoxC.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);
#包括
结构文件结构{
字符**头阵列;
int numberHeaderRows;
字符数组;
int numberDateRows;
int**valueArray;
字符***字符串数组;
int numberValueRows;
整数数值列;
};
外部结构文件结构**加载的文件;
无效加载文件(常量字符*文件名、常量字符*分隔符、常量字符*xRegex、int xRegexColumn、int xColumn、int yColumn、int xRow、int yRowShift、bool collectString);
ToolBoxC.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;
}
#包括
#包括“ToolBoxC.h”
#定义MaxFiles 10000
结构文件结构**加载的文件;
无效加载文件(常量字符*文件名、常量字符*分隔符、常量字符*xRegex、int xRegexColumn、int xColumn、int yColumn、int xRow、int yRowShift、bool collectString){
静态int-nextIndex=0;
如果(loadedFiles==0)
loadedFiles=malloc(MaxFiles*sizeof(*loadedFiles));
struct FileStruct*file=malloc(sizeof(struct FileStruct));
文件->numberDateRows=xRow;
loadedFiles[nextIndex++]=文件;
}
快速测试方法

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)")
    }
}
func loadFilesTest()->Void{
对于变量i:Int32=0;i<10;++i{
加载文件(“,”“,”,0,0,0,i,0,true)
}
对于var j=0;j<10;++j{
let pointer=UnsafePointer(loadedFiles[j])
打印(“日期行数=\(指针.memory.numberDaterRows)”)
}
}

太棒了!非常感谢,罗伯特。我不完全理解,但它很好用。不客气,@user2156649。请随时提出任何问题;我会尽力回答。还有一个问题……内存管理。Swift多线程调用C函数loadfile()。许多文件也可以同时处理。在Swift部分中,我使用NSOperationQueue。我的问题是:malloc线程安全吗?它通常在没有问题的情况下工作,但有时会出现“malloc:**对象错误:无效指针从空闲列表中退出队列***在malloc\u error\u break中设置断点以进行调试”。