如何从斯威夫特调用C? [英] How to call C from Swift?

查看:97
本文介绍了如何从斯威夫特调用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从斯威夫特调用C程序?

Is there a way to call C routines from Swift?

很多的iOS /苹果库仅是C,我还是想能够调用的。

A lot of iOS / Apple libraries are C only and I'd still like to be able to call those.

例如,我希望能够从迅速调用objc运行库。

For example, I'd like to be able to call the objc runtime libraries from swift.

在特定的,你怎么弥补iOS的C头文件?

In particular, how do you bridge iOS C headers?

推荐答案

是的,你当然可以用苹果的C库交互。 <一href=\"https://developer.apple.com/library/$p$prelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html#//apple_ref/doc/uid/TP40014216-CH8-XID_13\">Here如何解释。


基本上,C型,C指针等被翻译成斯威夫特对象,例如一个C INT 斯威夫特是一个 CINT

Yes, you can of course interact with Apples C libraries. Here is explained how.
Basically, the C types, C pointers, etc are translated into Swift objects, for example a C int in Swift is a CInt.

我已经建立一个很小的例子,对于另一个问题,它可以作为一个小的解释,​​对如何C和雨燕之间的桥梁:

I've build a tiny example, for another question, which can be used as a little explanation, on how to bridge between C and Swift:

main.swift

import Foundation

var output: CInt = 0
getInput(&output)

println(output)



UserInput.c

#include <stdio.h>

void getInput(int *output) {
    scanf("%i", output);
}



cliinput桥接-Header.h

void getInput(int *output);

这里是原来的答案。

这篇关于如何从斯威夫特调用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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