快速访问C Struct [英] Swift access to C Struct
问题描述
我正在开发一个OS X Swift应用程序,用于解析cvs文件.它在Objective-C中成功运行.然后我改用Swift,为了提高性能,我用C开发了解析/导入引擎.它的速度是Swift或Objective-C的5倍-很好.但是我很难在C和Swift之间交换数据-尤其是使用Struct:
I am developing a OS X Swift app for parsing cvs files. It runs successfully in 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. But I have trouble to exchange the data between C and Swift - especially with Struct:
BridgingHeader:
#include "ToolBoxC.h"
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;
ToolBoxC.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;
}
所有解析的数据都存储在 struct FilesStruct filesContent
中.通过使用Swift的参数调用函数loadFile()来开始解析.很好解析也可以.但是如何从Swift访问 struct FilesStruct filesContent
中的数据?
All the parsed data are stored in struct FilesStruct filesContent
. The parsing is started by calling the function loadFile() with parameters from Swift. That works fine. Also the parsing is OK. But how can I access to the data in struct FilesStruct filesContent
from Swift?
谢谢,马蒂亚斯.
推荐答案
尝试一下:
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);
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;
}
快速测试方法
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)")
}
}
这篇关于快速访问C Struct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!