当前位置:   article > 正文

Swift、C和C++混编_swift c

swift c

 苹果推出Swift后,很快就受到程序猿的青睐,快速、安全、简洁的开发已经是一个大趋势了。如果在加上C和C++真的是天衣无缝了,完美结合。对于Swift调用C和C++,通常需要OC做桥接(oc调用c和c++),性能得不到优化。我们今天直接用Swift调用C使性能得到全面的提升。苹果号称和C一样快的Swift的demo我们来粒子。

  1. 测试环境:
  2. Xcode版本:11.0-beta   Swift版本:5.0
  • Swift直接调用C

我们直接进入主题,Swift调用C。先创建一个工程SwiftCDemo,然后创建一个C文件,命名为CModel,.h文件创建以下函数:

 

  1. #ifndef CModel_h
  2. #define CModel_h
  3. #include <stdio.h>
  4. void printHellow(void);
  5. int getRandomInt(void);
  6. #endif /* CModel_h */

在.c文件里实现该函数:

  1. #include "CModel.h"
  2. #include <stdlib.h>
  3. void printHellow(void){
  4. printf("hellow world,I am is C language");
  5. }
  6. int getRandomInt(void){
  7. return rand();
  8. }

我们定义两个c函数(c不是很厉害只能这么简单,会使用就好)。我们现在用Swift调用。

我们定义一个头文件,也就是对外(swift)的接口。我们创建一个.h文件,命名为CBridging.h。

  1. #ifdef __cplusplus
  2. extern "C" {
  3. #endif
  4. void printHellow(void);
  5. int getRandomInt(void);
  6. #ifdef __cplusplus
  7. }
  8. #endif

对于上边的定义如果不是很明白,可以自己在网上查查,有很多讲解,我在这里就不多说啦。在创建的时候会有提示是否创建桥接文件,记得点击是,如果点击了否,那么就要自己创建了,怎么创建呢。推荐一篇文章

我们需要在桥接文件中导入CBridging.h。然后我们在默认的ViewController.swift里运行。

  1. class ViewController: UIViewController {
  2. override func viewDidLoad() {
  3. super.viewDidLoad()
  4. // Do any additional setup after loading the view.
  5. print("\n----------------------------\n\n")
  6. printHellow();
  7. let cRandomInt = getRandomInt();
  8. print("\n")
  9. print("收到C函数的随机数是:\(cRandomInt)");
  10. }
  11. }

打印log日志为:

  1. ----------------------------
  2. hellow world,I am is C languagerecive =16807

到此,我们已经成功的调用的C语言。对于c++呢我们也是这个方法。

  • Swift调用C++

swift要调用c++和c是一样的。我们先创建一个c++文件,命名为CPPerson。

在.hpp里声明一个类:

  1. #include <iostream>
  2. using namespace std;
  3. class CPPerson {
  4. string name;
  5. int age;
  6. bool isNan;
  7. public:
  8. CPPerson();
  9. CPPerson(const char * name,int age, bool isNan);
  10. ~CPPerson();
  11. void printPersonInfo();
  12. const char* getPersonName();
  13. };

在.cpp里实现这个类。

  1. CPPerson::CPPerson(){
  2. this->name = "管理员";
  3. this->age = 20;
  4. this->isNan = true;
  5. }
  6. CPPerson::CPPerson(const char * name,int age, bool isNan){
  7. this->name = name;
  8. this->age = age;
  9. this->isNan = isNan;
  10. }
  11. void CPPerson::printPersonInfo(){
  12. cout << "i am is" << name << ",my age is "<< age;
  13. if (isNan) {
  14. cout << "i am a body";
  15. }else{
  16. cout << "i am a girl";
  17. }
  18. }
  19. const char* CPPerson::getPersonName() {
  20. return name.data();
  21. }
  22. CPPerson::~CPPerson(){
  23. cout << "释放资源";
  24. }

我们在CBridging.h中加上c++接口:

  1. CPersonModel create();
  2. CPersonModel createBy(const char* name,int age);
  3. void printPersonInfo(CPersonModel model);
  4. const char* getPersonName(CPersonModel model);
  5. void destoryModel(CPersonModel model);

记得加上声明:

typedef void* CPersonModel;

我们需要在.cpp文件里实现这些函数:

  1. CPersonModel create() {
  2. return new CPPerson();
  3. }
  4. CPersonModel createBy(const char* name,int age){
  5. return new CPPerson(name,age,true);
  6. }
  7. void printPersonInfo(CPersonModel model) {
  8. CPPerson *p = (CPPerson*)model;
  9. p->printPersonInfo();
  10. }
  11. const char* getPersonName(CPersonModel model){
  12. CPPerson *p = (CPPerson*)model;
  13. return p->getPersonName();
  14. }
  15. void destoryModel(CPersonModel model){
  16. CPPerson *p = (CPPerson*)model;
  17. delete p;
  18. }

在ViewController.swift调用

  1. let person = createBy("peter", 14);
  2. printPersonInfo(person);
  3. let cName = getPersonName(person);
  4. let name = String(cString: cName!);
  5. print("fetch name is:\(name)");
  6. destoryModel(person);
  • C调用Swift

swift调用C,那么C也能调用Swift。我们来看看是怎么调用的。

在桥接文件里声明一个回调函数:

extern void(^ __nonnull swiftFuncIMP)(int);

在CModel.h文件里初始化

void(^ __nonnull swiftFuncIMP)(int) = NULL;

在swift里声明一个函数,并赋值给这个回调函数。

  1. func swiftFunImplement(a:Int32) -> Void {
  2. print("收到一个c函数的Int值->\(a)");
  3. }

在写个测试调用的函数。我们就可以直接调用了,说了这么多,相信有点头绪了。

另外还有点,c++调用swift,这个留下来思考。最后附上demo地址。欢迎指正。

demo地址。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/223738
推荐阅读
相关标签
  

闽ICP备14008679号