赞
踩
苹果推出Swift后,很快就受到程序猿的青睐,快速、安全、简洁的开发已经是一个大趋势了。如果在加上C和C++真的是天衣无缝了,完美结合。对于Swift调用C和C++,通常需要OC做桥接(oc调用c和c++),性能得不到优化。我们今天直接用Swift调用C使性能得到全面的提升。苹果号称和C一样快的Swift的demo我们来粒子。
- 测试环境:
- Xcode版本:11.0-beta Swift版本:5.0
我们直接进入主题,Swift调用C。先创建一个工程SwiftCDemo,然后创建一个C文件,命名为CModel,.h文件创建以下函数:
- #ifndef CModel_h
- #define CModel_h
-
- #include <stdio.h>
-
- void printHellow(void);
- int getRandomInt(void);
-
- #endif /* CModel_h */
在.c文件里实现该函数:
- #include "CModel.h"
- #include <stdlib.h>
-
- void printHellow(void){
- printf("hellow world,I am is C language");
- }
-
- int getRandomInt(void){
- return rand();
- }
我们定义两个c函数(c不是很厉害只能这么简单,会使用就好)。我们现在用Swift调用。
我们定义一个头文件,也就是对外(swift)的接口。我们创建一个.h文件,命名为CBridging.h。
- #ifdef __cplusplus
- extern "C" {
- #endif
- void printHellow(void);
- int getRandomInt(void);
-
- #ifdef __cplusplus
- }
- #endif
对于上边的定义如果不是很明白,可以自己在网上查查,有很多讲解,我在这里就不多说啦。在创建的时候会有提示是否创建桥接文件,记得点击是,如果点击了否,那么就要自己创建了,怎么创建呢。推荐一篇文章。
我们需要在桥接文件中导入CBridging.h。然后我们在默认的ViewController.swift里运行。
- class ViewController: UIViewController {
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
-
- print("\n----------------------------\n\n")
- printHellow();
- let cRandomInt = getRandomInt();
- print("\n")
- print("收到C函数的随机数是:\(cRandomInt)");
- }
-
-
- }
-

打印log日志为:
- ----------------------------
-
-
- hellow world,I am is C languagerecive =16807
到此,我们已经成功的调用的C语言。对于c++呢我们也是这个方法。
swift要调用c++和c是一样的。我们先创建一个c++文件,命名为CPPerson。
在.hpp里声明一个类:
- #include <iostream>
-
- using namespace std;
-
- class CPPerson {
- string name;
- int age;
- bool isNan;
-
- public:
-
- CPPerson();
- CPPerson(const char * name,int age, bool isNan);
- ~CPPerson();
-
- void printPersonInfo();
- const char* getPersonName();
-
- };

在.cpp里实现这个类。
-
- CPPerson::CPPerson(){
- this->name = "管理员";
- this->age = 20;
- this->isNan = true;
- }
-
- CPPerson::CPPerson(const char * name,int age, bool isNan){
- this->name = name;
- this->age = age;
- this->isNan = isNan;
- }
-
- void CPPerson::printPersonInfo(){
- cout << "i am is" << name << ",my age is "<< age;
- if (isNan) {
- cout << "i am a body";
- }else{
- cout << "i am a girl";
- }
- }
- const char* CPPerson::getPersonName() {
- return name.data();
- }
-
- CPPerson::~CPPerson(){
- cout << "释放资源";
- }

我们在CBridging.h中加上c++接口:
- CPersonModel create();
- CPersonModel createBy(const char* name,int age);
- void printPersonInfo(CPersonModel model);
- const char* getPersonName(CPersonModel model);
- void destoryModel(CPersonModel model);
记得加上声明:
typedef void* CPersonModel;
我们需要在.cpp文件里实现这些函数:
- CPersonModel create() {
- return new CPPerson();
- }
- CPersonModel createBy(const char* name,int age){
- return new CPPerson(name,age,true);
- }
- void printPersonInfo(CPersonModel model) {
- CPPerson *p = (CPPerson*)model;
- p->printPersonInfo();
- }
- const char* getPersonName(CPersonModel model){
- CPPerson *p = (CPPerson*)model;
- return p->getPersonName();
- }
- void destoryModel(CPersonModel model){
- CPPerson *p = (CPPerson*)model;
- delete p;
- }

在ViewController.swift调用
- let person = createBy("peter", 14);
- printPersonInfo(person);
- let cName = getPersonName(person);
- let name = String(cString: cName!);
- print("fetch name is:\(name)");
- destoryModel(person);
swift调用C,那么C也能调用Swift。我们来看看是怎么调用的。
在桥接文件里声明一个回调函数:
extern void(^ __nonnull swiftFuncIMP)(int);
在CModel.h文件里初始化
void(^ __nonnull swiftFuncIMP)(int) = NULL;
在swift里声明一个函数,并赋值给这个回调函数。
- func swiftFunImplement(a:Int32) -> Void {
- print("收到一个c函数的Int值->\(a)");
- }
在写个测试调用的函数。我们就可以直接调用了,说了这么多,相信有点头绪了。
另外还有点,c++调用swift,这个留下来思考。最后附上demo地址。欢迎指正。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。