Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 用C投掷斯威夫特可以接住的项目_Ios_C_Swift - Fatal编程技术网

Ios 用C投掷斯威夫特可以接住的项目

Ios 用C投掷斯威夫特可以接住的项目,ios,c,swift,Ios,C,Swift,我使用带有C代码的静态库,该库具有静态回调方法。指向该方法的指针被指定给变量。每次库调用此函数时,我也需要使用swift处理此事件。一个最简单的例子是: static void onEventXYZ(int x, int y, struct z); int anFunction(){ libraryFile.attribute = &onEventXYZ; } //a C callback function static void onEventXYZ(int x, int

我使用带有C代码的静态库,该库具有静态回调方法。指向该方法的指针被指定给变量。每次库调用此函数时,我也需要使用swift处理此事件。一个最简单的例子是:

static void onEventXYZ(int x, int y, struct z);

int anFunction(){
    libraryFile.attribute = &onEventXYZ;
}


//a C callback function
static void onEventXYZ(int x, int y, struct z){
    //function body
}
我这里的问题是,我不知道如何对swift类中的回调做出反应。值得一提的是,当应用程序可能在后台时,我还需要对此回调做出反应


有可能抛出swift能够捕获和处理的事件或信号吗?

假设您有一个函数,该函数将指针指向该回调函数

void doSomething(void (*callback)(int, int)); // int parameters just for example
。。。它被翻译成Swift,如下所示:

func doSomething(callback: @convention(c) (Int, Int) -> Void)
这意味着您可以将任何函数用作回调函数,只要它与
@convention(c)
标准兼容:

doSomething {
    print($0)
    print($1)
}

// or

func onSomething(i: Int, j: Int) {
    print(i)
    print(j)
}

doSomething(onSomething)

// or

let onSomething: (Int, Int) -> Void = { i, j in
    print(i)
    print(j)
}

doSomething(onSomething)

好吧,看来我不够精确,我会更新这个问题。我得到了一个分配给属性的静态函数上的指针。当库触发特定事件时,我需要触发swiftcode和c代码